首页/ AI 技能 / @skilllib/skill-2014279404250148866
Skill站外收录 @skilllib/skill-2014279404250148866

language

确立 TypeScript 类型安全与可维护性的现代实践标准,涵盖接口与类型定义规范、泛型复用、空值安全处理、不可变性约束、字面量联合与判别式联合等核心模式,并规避 any、enum、非空断言等高风险用法。

0.0 (0) 524 次安装 原作者 编程导航
收录于 2026-01-22
测试与安全 AI 技能

Skills.MD

# TypeScript Language Patterns ## **Priority: P0 (CRITICAL)** Modern TypeScript standards for type-safe, maintainable code. ## Implementation Guidelines - **Type Annotations**: Explicit params/returns. Infer locals. - **Interfaces vs Types**: `interface` for APIs. `type` for unions. - **Strict Mode**: `strict: true`. - **Null Safety**: `?.` and `??`. - **Enums**: Literal unions or `as const`. - **Generics**: Reusable, type-safe code. - **Type Guards**: `typeof`, `instanceof`, predicates. - **Utility Types**: `Partial`, `Pick`, `Omit`, `Record`. - **Immutability**: `readonly` arrays/objects. - **Const Assertions**: `as const` and `satisfies`. - **Template Literals**: `on${Capitalize<string>}`. - **Discriminated Unions**: Literal `kind` property. - **Advanced**: Mapped, Conditional, Indexed types. - **Branded Types**: `string & { __brand: 'Id' }`. ## Anti-Patterns - **No `any`**: Use `unknown`. - **No `Function`**: Use signature `() => void`. - **No `enum`**: Runtime cost. - **No `!`**: Use narrowing. ## Code ```typescript // Branded Type type UserId = string & { __brand: 'Id' }; // Satisfies (Validate + Infer) const cfg = { port: 3000 } satisfies Record<string, number>; // Discriminated Union type Result<T> = { kind: 'ok'; data: T } | { kind: 'err'; error: Error }; ``` ## Reference & Examples For advanced type patterns and utility types: See [references/REFERENCE.md](references/REFERENCE.md). ## Related Topics best-practices | security | tooling

评论 0

还没有评论,来抢沙发。