typescript
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTypeScript
TypeScript
Specification
规范
The words , , , , , , , , , and are interpreted as described in RFC 2119.
<prerequisite>
This skill extends the JavaScript preference skill. You MUST load `javascript` first for naming,
module style, async patterns, and general JavaScript conventions.
</prerequisite>
MUSTMUST NOTREQUIREDSHALLSHALL NOTSHOULDSHOULD NOTRECOMMENDEDMAYOPTIONALTypes encode intent. Let the compiler prove the rest.
You MUST use TypeScript 7.0 as the baseline. You MUST carry TypeScript 6.0 migration guidance forward:
7.0 is expected to match 6.0 checking and command-line behavior, adopt the 6.0 defaults,
and hard-error on constructs and flags deprecated in 6.0.
文中的 、、、、、、、、 和 术语按 RFC 2119 中的定义解释。
<prerequisite>
本技能扩展自JavaScript规范技能。你必须先加载`javascript`技能,以遵循命名规范、模块风格、异步模式及通用JavaScript约定。
</prerequisite>
MUSTMUST NOTREQUIREDSHALLSHALL NOTSHOULDSHOULD NOTRECOMMENDEDMAYOPTIONAL类型承载意图,其余交由编译器验证。
你必须以TypeScript 7.0为基准版本。你必须延续TypeScript 6.0的迁移指导原则:7.0版本需匹配6.0的检查逻辑与命令行行为,沿用6.0的默认配置,并对6.0中已废弃的语法结构和编译标记触发硬错误。
References
参考资料
| Topic | Reference | Load when |
|---|---|---|
| Configuration and TS 6-to-7 migration | | Updating tsconfig, module resolution, compiler directives, native tooling |
| Generics and type-level programming | | Designing reusable generic APIs, utility types, mapped/conditional/template literal types |
| Narrowing and type guards | | Modeling variants, validating unknown values, writing exhaustive code |
| Patterns and declarations | | Choosing interfaces/types/enums/classes/brands/overloads/callback signatures |
| 主题 | 参考文档 | 适用场景 |
|---|---|---|
| 配置与TS 6到7迁移 | | 更新tsconfig、模块解析、编译器指令、原生工具链 |
| 泛型与类型级编程 | | 设计可复用的泛型API、工具类型、映射/条件/模板字面量类型 |
| 类型收窄与类型守卫 | | 建模变体、验证未知值、编写穷尽式代码 |
| 模式与声明 | | 选择接口/类型别名/枚举/类/品牌类型/重载/回调签名 |
Baseline
基准原则
- You MUST treat TypeScript 7.0 as stable project direction. The native Go compiler and language service are the expected direction for type checking, editor tooling, and performance-sensitive validation.
- You MUST use 6.0 as the compatibility bridge: code that passes 6.0 with stable type ordering and without deprecated flags SHOULD be ready for 7.0.
- You SHOULD prefer explicit, domain-shaped types over clever type-level machinery. Stay at the simplest type complexity tier that proves the behavior.
- You MUST match the project's established conventions first. If a project contradicts this skill, follow the project locally and flag the divergence once.
- 你必须将TypeScript 7.0视为项目的稳定技术方向。原生Go编译器与语言服务是类型检查、编辑器工具链及性能敏感型验证的首选方案。
- 你必须以6.0版本作为兼容性过渡桥梁:通过6.0版本检查、具备稳定类型排序且未使用废弃标记的代码,应可直接适配7.0版本。
- 相较于复杂的类型级实现,你应优先选择明确的、贴合业务域的类型。采用能验证行为的最简类型复杂度层级即可。
- 你必须优先遵循项目已确立的规范。若项目规范与本技能冲突,先遵循本地项目规范,并一次性标记差异点。
Type Safety
类型安全
- Strict mode is REQUIRED. TS 7.0 defaults to strict checking; keep explicit in shared configs when clarity helps.
strict: true - You MUST use for untrusted values. Narrow API, JSON, user input, and caught values before use. Temporary
unknownis acceptable only for migrations or mocks, with a short comment explaining why.any - You SHOULD avoid non-null assertions. Narrow instead. If is truly needed, explain the invariant near the assertion.
! - You SHOULD prefer annotations and over assertions. Use
satisfiesto validate literals without widening; use annotations where intentional widening or public API shape matters. Usesatisfiesonly when you know more than the compiler.as - You MUST use and
import typefor type-only imports and re-exports.export type - You MUST NOT suppress type errors casually. Prefer fixing the type. Use only for deliberate invalid examples or tests, with a comment. Do not use
@ts-expect-erroror@ts-ignorein production code.@ts-nocheck
- 必须启用严格模式。 TS 7.0默认开启严格检查;若需提升清晰度,在共享配置中显式设置
strict: true - 必须对不可信值使用类型。 在使用API返回值、JSON数据、用户输入及捕获的错误值前,先进行类型收窄。仅在迁移或模拟场景下可临时使用
unknown,并需添加简短注释说明原因。any - 应避免非空断言。 优先使用类型收窄。若确实需要使用,需在断言附近注释说明不变条件。
! - 相较于类型断言,应优先使用类型注解和。 使用
satisfies验证字面量类型而不进行类型拓宽;在需要刻意拓宽类型或定义公共API形态时使用类型注解。仅当你比编译器掌握更多信息时,才使用satisfies进行断言。as - 必须使用和
import type用于仅类型导入与重导出。export type - 不得随意抑制类型错误。 优先修复类型问题。仅在编写故意无效的示例或测试时使用,并添加注释。生产代码中禁止使用
@ts-expect-error或@ts-ignore。@ts-nocheck
Modeling
类型建模
- You SHOULD use discriminated unions for variants. Use a ,
kind, or similarly stable literal discriminator and exhaustive handling.type - You SHOULD use interfaces for object shapes and type aliases for everything else. Use for unions, intersections, tuples, function types, mapped types, and conditional types.
type - You SHOULD use literal unions or maps for simple constants. Use string enums only when a real runtime object or namespace is useful. Avoid implicit numeric enums and mixed enum members.
as const - Branded types for nominal safety. Use them for domain IDs, validated strings, and units that MUST NOT be mixed.
- Keep nullability at use sites. Prefer over spreading
function find(): User | nullaliases through the codebase.MaybeUser - Use for dictionary-shaped data. Prefer specific key unions when possible.
Record<K, V>
- 应使用区分联合类型建模变体。 使用、
kind或类似的稳定字面量作为区分符,并实现穷尽式处理。type - 对象形态优先使用接口,其他场景优先使用类型别名。 使用定义联合类型、交叉类型、元组、函数类型、映射类型及条件类型。
type - 简单常量优先使用字面量联合类型或映射。 仅当需要真实运行时对象或命名空间时,才使用字符串枚举。避免隐式数字枚举和混合类型枚举成员。
as const - 使用品牌类型实现名义类型安全。 将其用于业务域ID、已验证字符串及不可混用的单位类型。
- 在使用站点处理可空性。 优先选择,而非在整个代码库中传播
function find(): User | null这类别名。MaybeUser - 使用定义字典形态数据。 若可能,优先使用具体的键联合类型。
Record<K, V>
Generics
泛型
- You SHOULD let inference work. Do not specify type arguments when the compiler can infer them.
- You SHOULD constrain generics with when the implementation requires structure.
extends - You SHOULD avoid return-type-only generics; callers SHOULD NOT have to guess type arguments.
- You SHOULD use built-in utility types (,
Partial,Pick,Omit,Record,ReturnType,Parameters,Awaited) instead of hand-rolled equivalents.NoInfer - You SHOULD use when one parameter MUST be checked against another parameter's inferred type without driving the inference itself.
NoInfer<T> - Reserve conditional, mapped, template literal, and recursive types for library/framework code or places where they remove more complexity than they add.
- 应让编译器自动推断类型。当编译器可自行推断时,无需显式指定类型参数。
- 当实现需要特定结构时,应使用约束泛型。
extends - 应避免仅用于返回值的泛型;调用者不应需要猜测类型参数。
- 应使用内置工具类型(、
Partial、Pick、Omit、Record、ReturnType、Parameters、Awaited) 而非手动实现等效类型。NoInfer - 当一个参数必须与另一个参数的推断类型进行校验,但又不希望驱动推断过程时,应使用。
NoInfer<T> - 仅在库/框架代码中,或能减少整体复杂度的场景下,使用条件类型、映射类型、模板字面量类型及递归类型。
Configuration
配置
- TS 7.0 defaults MUST be treated as normative: is on,
strictdefaults towardmodule, andesnextdefaults toward the stable ECMAScript version immediately beforetarget.esnext - You SHOULD prefer modern configuration:
- Bundled apps: ,
module: "preserve",moduleResolution: "bundler".noEmit: true - Direct Node output: , with package
module: "nodenext"and emitted file extensions handled deliberately.type - Use ,
verbatimModuleSyntax,isolatedModules, and explicitmoduleDetection: "force".types
- Bundled apps:
- You MUST NOT depend on TS 6.0-deprecated paths in new work: legacy ES5 targeting, classic/node10-style resolution, as a lookup root,
baseUrl,import ... asserts, or AMD/UMD/SystemJS module targets.outFile - You SHOULD use while validating 6.0-to-7.0 migrations when type-display or declaration-order stability matters.
stableTypeOrdering
- TS 7.0的默认配置必须视为标准:默认开启,
strict默认趋向module,esnext默认趋向target之前的稳定ECMAScript版本。esnext - 应优先使用现代配置:
- 打包应用:,
module: "preserve",moduleResolution: "bundler"。noEmit: true - 直接输出Node代码:,需明确处理package的
module: "nodenext"字段及输出文件扩展名。type - 使用、
verbatimModuleSyntax、isolatedModules及显式moduleDetection: "force"配置。types
- 打包应用:
- 新代码中不得依赖TS 6.0已废弃的配置路径:遗留ES5目标版本、classic/node10风格解析、将作为查找根、
baseUrl、import ... asserts,或AMD/UMD/SystemJS模块目标。outFile - 当类型显示或声明顺序稳定性很重要时,在验证6.0到7.0的迁移过程中应使用。
stableTypeOrdering
Application
实践应用
When writing TypeScript:
- You MUST apply these conventions without narrating each one.
- You MUST keep exported functions, public methods, and package boundaries explicitly typed.
- You SHOULD keep local implementation types inferred when inference is clear.
- You MUST use runtime validation at trust boundaries; types do not validate data at runtime.
When reviewing TypeScript:
- You MUST lead with concrete bugs, unsoundness, or migration risks.
- You SHOULD show the smallest correction inline.
- You MUST separate style preferences from type-safety issues.
编写TypeScript代码时:
- 必须直接应用这些规范,无需逐一说明。
- 必须为导出函数、公共方法及包边界显式添加类型注解。
- 当类型推断清晰时,应让本地实现类型自动推断。
- 必须在信任边界处进行运行时验证;类型无法在运行时验证数据。
评审TypeScript代码时:
- 必须先关注具体的bug、类型不安全问题或迁移风险。
- 应展示最小化的内联修正方案。
- 必须将风格偏好与类型安全问题区分开。