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危険な非 null アサーションを回避
  6. 06失敗しうる関数に結果ユニオンを使用

活用シーン

  1. 01TypeScriptの差分にある型のリスクをレビュー
  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).

コメント