mcpField
T
typescript-best-practices

typescript-best-practices

github.com/yeachan-heo其他40.7k8 次安装今天更新
代码仓库

关于 typescript-best-practices

一项用于 TypeScript 实现与代码审查的类型系统技能,强调让无效状态难以表达,并通过类型化边界处理外部数据。

主要功能

  1. 01优先使用判别联合而非布尔标志
  2. 02用 `satisfies` 检查配置字面量
  3. 03将外部数据解析为类型化结果
  4. 04为含义不同的原始类型添加品牌
  5. 05避免不安全的非空断言
  6. 06用结果联合表示可失败函数

适用场景

  1. 01审查 TypeScript 差异中的类型风险
  2. 02安全建模外部 JSON 或 CLI 输入
  3. 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

来自项目 README

README 暂无中文译文,以下为英文原文;中文介绍见上方。

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}. Exhaustive switch on kind turns missing cases into compile errors.
  • satisfies for configuration literals. {...} satisfies Config keeps 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 any re-cast. unknown in, typed out.
  • Brand primitives that carry meaning. type SessionId = string & {__brand:'SessionId'} when mixing two strings 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 T plus a thrown maybe; callers must handle both arms to compile.
  • readonly by 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