typescript-v6

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Skill: 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
    typescript
    (this repo:
    @typescript/typescript6
    ^6), not
    npm view typescript
    (may already be 7)
  • 6.0 is the last JavaScript-based compiler; 7.0 is native (Go).
    @typescript/native
    may sit beside
    tsc
    ; it is not this skill’s major
  • Strict mode (
    "strict": true
    )
  • Target ES2020+ (6.0 defaults trend toward
    esnext
    /
    es2025
    )
  • Options deprecated in 6.0 can be ignored with
    "ignoreDeprecations": "6.0"
    until 7.0 removes them. Prefer fixing deprecations: https://devblogs.microsoft.com/typescript/announcing-typescript-6-0/
  • 文件夹主版本以
    package.json
    中的
    typescript
    为准(此仓库:
    @typescript/typescript6
    ^6),而不是
    npm view typescript
    (后者可能已经是 7)
  • 6.0 是最后一个基于 JavaScript 的编译器;7.0 是基于 Go 的原生编译器。
    @typescript/native
    可能与
    tsc
    并存,但它并非此 Skill 的主要关注范围
  • 严格模式(
    "strict": true
    )
  • 目标为 ES2020+(6.0 的默认配置趋向于
    esnext
    /
    es2025
    )
  • 在 7.0 移除这些选项之前,可通过
    "ignoreDeprecations": "6.0"
    暂时忽略 6.0 中已弃用的选项。优先修复弃用项:https://devblogs.microsoft.com/typescript/announcing-typescript-6-0/

Principles

原则

  • Use conditional types for type selection based on conditions
  • Use mapped types for systematic object type transformations
  • Use type guards (
    value is Type
    ) for runtime checking with type narrowing
  • Use discriminated unions for type-safe state machines with exhaustiveness checking
  • Use branded types to prevent primitive mixing
  • Prefer
    unknown
    over
    any
    for type safety
  • 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 (
    "strict": true
    in
    tsconfig.json
    )
  • Use
    unknown
    instead of
    any
    for untyped values
  • Use type guards (
    value is Type
    ) instead of type assertions when possible
  • 启用严格模式(在
    tsconfig.json
    中设置
    "strict": true
    )
  • 对无类型值使用
    unknown
    ,而不是
    any
  • 尽可能使用类型守卫(
    value is Type
    ),而不是类型断言

SHOULD

应该

  • Use
    type
    for unions/utilities,
    interface
    for object shapes
  • 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
  • 联合类型和工具类型使用
    type
    ,对象结构使用
    interface
  • 保持类型可组合且浅层
  • 使用
    readonly
    标记不可变数据结构
  • 使用品牌类型防止原始类型混用
  • 在采用 TypeScript 7 原生编译器之前解决 6.0 的弃用项

AVOID

避免

  • Using
    any
    (use
    unknown
    with type guards)
  • Type assertions without validation
  • Overusing generics (only when types truly vary)
  • Deep type nesting (slow compilation, hard to debug)
  • Import assertions (
    assert { type: ... }
    ); use
    with
    import attributes
  • 使用
    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 - 类型安全状态机

Resources

资源