关于 typescript-best-practices
一项用于 TypeScript 实现与代码审查的类型系统技能,强调让无效状态难以表达,并通过类型化边界处理外部数据。
主要功能
- 01优先使用判别联合而非布尔标志
- 02用 `satisfies` 检查配置字面量
- 03将外部数据解析为类型化结果
- 04为含义不同的原始类型添加品牌
- 05避免不安全的非空断言
- 06用结果联合表示可失败函数
适用场景
- 01审查 TypeScript 差异中的类型风险
- 02安全建模外部 JSON 或 CLI 输入
- 03表达领域状态,避免可选字段歧义
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ yeachan-heo/ oh-my-claudecode --skill typescript-best-practices安装后在 Claude Code 中输入/typescript-best-practices确认已加载。
附带文件references/patterns.md
README 原文English
来自项目 READMEREADME 暂无中文译文,以下为英文原文;中文介绍见上方。
TypeScript Best Practices
The type system is the cheapest place to make illegal states unrepresentable
(principles skill, Type system discipline). Concrete forms:
- Discriminated unions over boolean flags.
type Ev = {kind:'click'; x:number} | {kind:'focus'}— not{isClick:boolean; x?:number}. Exhaustiveswitchonkindturns missing cases into compile errors. satisfiesfor configuration literals.{...} satisfies Configkeeps literal inference while checking the shape.- Parse, don't validate, at boundaries. External data (JSON, env, CLI args, API responses) passes through one parsing function returning a typed result; downstream code takes the parsed type, never
anyre-cast.unknownin, typed out. - Brand primitives that carry meaning.
type SessionId = string & {__brand:'SessionId'}when mixing twostrings is a realistic bug. - No non-null assertions on external data.
!is allowed only where a check on a previous line proves the value; at boundaries it is a runtime crash with compile-time approval. - Return types that prevent misuse. A function that can fail returns a result union, not
Tplus a thrown maybe; callers must handle both arms to compile. readonlyby default on shared shapes, mutation as an explicit opt-in.
Reviewing
When reviewing TS, the question per diff hunk is not "does it compile" but
"which illegal state did this type just permit" — flag flags-over-unions,
boundary anys, and unbranded overlapping primitives.
Rules
- Discipline applies at the boundary and the domain model; inside a single function, pragmatism wins.
- Never introduce a type abstraction with one caller and no second one
coming (
minimal-code-discipline).
评论
支持 Markdown