SKILL.md
只读
名称
typescript-pro
描述
实现高级 TypeScript 类型系统,构建自定义类型守卫、工具类型与品牌类型(Branded Types),并配置 tRPC 以实现端到端类型安全。适用于需要复杂泛型、条件或映射类型、可辨识联合(Discriminated Unions)、Monorepo 架构或借助 tRPC 实现全栈类型安全的 TypeScript 应用开发。
TypeScript Pro
核心工作流
- 分析类型架构 —— 审查 tsconfig 配置、类型覆盖率及构建性能
- 设计类型优先的 API —— 创建品牌类型、泛型与工具类型
- 类型安全实现 —— 编写类型守卫、可辨识联合与条件类型;运行
tsc --noEmit以在继续前捕获类型错误 - 优化构建 —— 配置项目引用(Project References)、增量编译与 Tree Shaking;改动后重新运行
tsc --noEmit确保零错误 - 测试类型 —— 使用
type-coverage等工具确认类型覆盖率;验证所有公共 API 均显式声明返回类型;反复迭代步骤 3~4 直至通过所有检查
参考指南
根据具体上下文加载详细指南:
| 主题 | 参考文档 | 加载时机 |
|---|---|---|
| 高级类型 | references/advanced-types.md |
使用泛型、条件类型、映射类型、模板字面量类型 |
| 类型守卫 | references/type-guards.md |
进行类型收窄、可辨识联合、断言函数 |
| 工具类型 | references/utility-types.md |
使用 Partial、Pick、Omit、Record 及自定义工具类型 |
| 配置项 | references/configuration.md |
调整 tsconfig 选项、严格模式、项目引用 |
| 设计模式 | references/patterns.md |
实现构建器模式、工厂模式、类型安全的 API |
代码示例
品牌类型(Branded Types)
// 用于领域建模的品牌类型
type Brand<T, B extends string> = T & { readonly __brand: B };
type UserId = Brand<string, "UserId">;
type OrderId = Brand<number, "OrderId">;
const toUserId = (id: string): UserId => id as UserId;
const toOrderId = (id: number): OrderId => id as OrderId;
// 使用方式 —— 在编译期防止误混用不同 ID
function getOrder(userId: UserId, orderId: OrderId) { /* ... */ }
可辨识联合与类型守卫
type LoadingState = { status: "loading" };
type SuccessState = { status: "success"; data: string[] };
type ErrorState = { status: "error"; error: Error };
type RequestState = LoadingState | SuccessState | ErrorState;
// 类型谓词守卫
function isSuccess(state: RequestState): state is SuccessState {
return state.status === "success";
}
// 结合可辨识联合的穷尽性 switch 检查
function renderState(state: RequestState): string {
switch (state.status) {
case "loading": return "Loading…";
case "success": return state.data.join(", ");
case "error": return state.error.message;
default: {
const _exhaustive: never = state;
throw new Error(`Unhandled state: ${_exhaustive}`);
}
}
}
自定义工具类型
// 深度 Readonly —— 递归冻结嵌套对象
type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K];
};
// 互斥必填项 —— 在一组键中精确指定一个必填
type RequireExactlyOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>> &
{ [K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, never>> }[Keys];
推荐的 tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"exactOptionalPropertyTypes": true,
"isolatedModules": true,
"declaration": true,
"declarationMap": true,
"incremental": true,
"skipLibCheck": false
}
}
约束条件
必须做(MUST DO)
- 开启严格模式及所有编译器严格校验标志
- 遵循类型优先(Type-first)的 API 设计
- 采用品牌类型(Branded Types)进行领域建模
- 使用
satisfies运算符进行类型校验 - 使用可辨识联合构造状态机
- 在类型谓词中使用带有注解(Annotated)的模式
- 为库项目生成声明文件(.d.ts)
- 针对类型推导进行性能优化
严禁做(MUST NOT DO)
- 无合理正当理由使用显式
any - 遗漏公共 API 的类型覆盖
- 混用纯类型导入与值导入
- 禁用严格空值检查(strictNullChecks)
- 无必要时滥用
as类型断言 - 忽视编译器性能警告
- 跳过声明文件生成
- 使用 enum 枚举(优先使用带有
as const的对象)
输出模板
在实现 TypeScript 功能时,需提供:
- 类型定义(interface、type、泛型)
- 包含类型守卫的具体实现
- 必要时的 tsconfig 配置
- 关于类型设计决策的简要说明
知识领域
TypeScript 5.0+、泛型、条件类型、映射类型、模板字面量类型、可辨识联合、类型守卫、品牌类型、tRPC、项目引用(Project References)、增量编译、声明文件、const 断言、satisfies 运算符




