typescript-v6
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: typescript
Skill:typescript
Scope
范围
- Applies to: TypeScript 6.0+ type system features, type-safe APIs, complex domain models, inference, compile-time guarantees
- Does NOT cover: Basic syntax, framework-specific patterns, runtime validation (use Zod separately)
- 适用于:TypeScript 6.0+ 类型系统功能、类型安全 API、复杂领域模型、类型推断、编译时保证
- 不涵盖:基础语法、框架特定模式、运行时验证(请单独使用 Zod)
Assumptions
前提条件
- Folder major tracks
package.json(this repo:typescript^6), not@typescript/typescript6(may already be 7)npm view typescript - 6.0 is the last JavaScript-based compiler; 7.0 is native (Go). may sit beside
@typescript/native; it is not this skill’s majortsc - Strict mode ()
"strict": true - Target ES2020+ (6.0 defaults trend toward /
esnext)es2025 - Options deprecated in 6.0 can be ignored with until 7.0 removes them. Prefer fixing deprecations: https://devblogs.microsoft.com/typescript/announcing-typescript-6-0/
"ignoreDeprecations": "6.0"
- 文件夹主版本以 中的
package.json为准(此仓库:typescript^6),而不是@typescript/typescript6(后者可能已经是 7)npm view typescript - 6.0 是最后一个基于 JavaScript 的编译器;7.0 是基于 Go 的原生编译器。可能与
@typescript/native并存,但它并非此 Skill 的主要关注范围tsc - 严格模式()
"strict": true - 目标为 ES2020+(6.0 的默认配置趋向于 /
esnext)es2025 - 在 7.0 移除这些选项之前,可通过 暂时忽略 6.0 中已弃用的选项。优先修复弃用项:https://devblogs.microsoft.com/typescript/announcing-typescript-6-0/
"ignoreDeprecations": "6.0"
Principles
原则
- Use conditional types for type selection based on conditions
- Use mapped types for systematic object type transformations
- Use type guards () for runtime checking with type narrowing
value is Type - Use discriminated unions for type-safe state machines with exhaustiveness checking
- Use branded types to prevent primitive mixing
- Prefer over
unknownfor type safetyany - Use type guards over type assertions ()
as Type - Keep types composable and shallow (avoid deep nesting)
- Mark immutable data structures as
readonly
- 使用条件类型(conditional types)根据条件进行类型选择
- 使用映射类型(mapped types)系统地转换对象类型
- 使用类型守卫(type guards,)进行运行时检查和类型收窄
value is Type - 使用可辨识联合类型(discriminated unions)构建支持穷尽性检查的类型安全状态机
- 使用品牌类型(branded types)防止原始类型混用
- 优先使用 而不是
unknown来保证类型安全any - 使用类型守卫,而不是类型断言()
as Type - 保持类型可组合且浅层,避免深层嵌套
- 使用 标记不可变数据结构
readonly
Constraints
约束
MUST
必须
- Enable strict mode (in
"strict": true)tsconfig.json - Use instead of
unknownfor untyped valuesany - Use type guards () instead of type assertions when possible
value is Type
- 启用严格模式(在 中设置
tsconfig.json)"strict": true - 对无类型值使用 ,而不是
unknownany - 尽可能使用类型守卫(),而不是类型断言
value is Type
SHOULD
应该
- Use for unions/utilities,
typefor object shapesinterface - Keep types composable and shallow
- Mark immutable data structures as
readonly - Use branded types to prevent primitive mixing
- Address 6.0 deprecations before adopting TypeScript 7 native
- 联合类型和工具类型使用 ,对象结构使用
typeinterface - 保持类型可组合且浅层
- 使用 标记不可变数据结构
readonly - 使用品牌类型防止原始类型混用
- 在采用 TypeScript 7 原生编译器之前解决 6.0 的弃用项
AVOID
避免
- Using (use
anywith type guards)unknown - Type assertions without validation
- Overusing generics (only when types truly vary)
- Deep type nesting (slow compilation, hard to debug)
- Import assertions (); use
assert { type: ... }import attributeswith
- 使用 (改用
any配合类型守卫)unknown - 使用未经验证的类型断言
- 过度使用泛型(仅在类型确实存在变化时使用)
- 深层类型嵌套(会导致编译速度变慢且难以调试)
- 使用导入断言();改用
assert { type: ... }导入属性with
Interactions
交互关系
- Works with zod for runtime validation with type inference
- Complements next for type-safe API routes
- Complements fastify for type-safe route schemas
- 可与 zod 配合使用,通过类型推断实现运行时验证
- 可与 next 配合,用于类型安全的 API 路由
- 可与 fastify 配合,用于类型安全的路由 Schema
Patterns
模式
Mapped Types
映射类型(Mapped Types)
Transform object types systematically:
typescript
type Partial<T> = { [P in keyof T]?: T[P] }
type Readonly<T> = { readonly [P in keyof T]: T[P] }
type Pick<T, K extends keyof T> = { [P in K]: T[P] }系统地转换对象类型:
typescript
type Partial<T> = { [P in keyof T]?: T[P] }
type Readonly<T> = { readonly [P in keyof T]: T[P] }
type Pick<T, K extends keyof T> = { [P in K]: T[P] }Type Guards
类型守卫(Type Guards)
Runtime type checking with type narrowing:
typescript
function isString(value: unknown): value is string {
return typeof value === 'string'
}
function isSuccess(result: Result): result is Success {
return result.status === 'success'
}通过类型收窄进行运行时类型检查:
typescript
function isString(value: unknown): value is string {
return typeof value === 'string'
}
function isSuccess(result: Result): result is Success {
return result.status === 'success'
}Branded Types
品牌类型(Branded Types)
Create nominal types for type safety:
typescript
type UserId = string & { readonly __brand: 'UserId' }
type PostId = string & { readonly __brand: 'PostId' }
function createUserId(id: string): UserId {
return id as UserId
}创建名义类型以提升类型安全性:
typescript
type UserId = string & { readonly __brand: 'UserId' }
type PostId = string & { readonly __brand: 'PostId' }
function createUserId(id: string): UserId {
return id as UserId
}Discriminated Unions
可辨识联合类型(Discriminated Unions)
Type-safe state machines with exhaustiveness checking:
typescript
type LoadingState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: string[] }
| { status: 'error'; error: Error }支持穷尽性检查的类型安全状态机:
typescript
type LoadingState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'success'; data: string[] }
| { status: 'error'; error: Error }References
参考资料
- Conditional Types - Type selection based on conditions
- Advanced Generics - Generic constraints and inference patterns
- Discriminated Unions - Type-safe state machines
- Conditional Types - 基于条件进行类型选择
- Advanced Generics - 泛型约束和推断模式
- Discriminated Unions - 类型安全状态机