typescript-best-practices
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTypeScript best practices
TypeScript最佳实践
Apply the type-system-discipline principle skill first; this skill grounds it in TypeScript syntax.
| Rule | Summary |
|---|---|
| Discriminated unions | Model variants with a |
| Branded types | Brand primitives with |
| External data is |
No | Every |
| Narrowing hierarchy | Discriminant switch > |
| Type guards | Must verify the claim. A lying guard is worse than |
| Exhaustiveness | Inline |
| Validates the value without widening literal types. |
| Boundary validation | Validate where data crosses in; trust types inside. See the boundary-discipline principle skill. |
| Schema-derived types | Reach for |
| Object args | Pass objects, not positional, so argument order is self-documenting. Skip on hot paths (per-frame render, tokenizers, parsers). |
Examples: .
references/patterns.md首先应用类型系统规范原则技能;该技能基于TypeScript语法。
| 规则 | 总结 |
|---|---|
| 可区分联合 | 使用 |
| 品牌类型 | 用 |
优先使用 | 外部数据类型为 |
禁止使用 | 每个 |
| 类型收窄优先级 | 判别式switch > |
| 类型守卫 | 必须验证断言内容。一个错误的守卫比 |
| 穷尽性检查 | 在默认分支中添加内联 |
优先使用 | 在不拓宽字面量类型的情况下验证值。 |
| 边界验证 | 在数据进入的地方进行验证;内部信任类型。请参阅边界规范原则技能。 |
| 模式派生类型 | 在声明新接口之前,优先使用 |
| 对象参数 | 传递对象而非位置参数,这样参数顺序是自文档化的。在热路径(每帧渲染、分词器、解析器)上可跳过此规则。 |
示例:.
references/patterns.md