Use when writing TypeScript that branches on a value carrying a modeled state — a discriminated union, a status/kind/type tag, or any state-machine state — to choose behavior or a return value, or when adding a variant to such a union. Also when reaching for an if/else-if chain, an assertNever helper, or a lookup map to consume a tagged union.
Ships in typescript-modeling. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
When a value models state as a discriminated union, branch on it with switch keyed on the discriminant, and make the default branch assign the value to never inline. A forgotten variant then becomes a typecheck failure that points at the exact switch, not a silent runtime fallthrough.
This skill is the consume side of a modeled type. REQUIRED SUB-SKILL: Use precise-type-modeling to model the union in the first place (kind/status/type discriminant, all states represented, no any/Record).
type FetchState =
| { kind: "idle" }
| { kind: "loading" }
| { kind: "ready"; value: string }
| { kind: "failed"; error: string };
function label(state: FetchState): string {
switch (state.kind) {
case "idle":
return "待機中";
case "loading":
return "読み込み中";
case "ready":
return state.value; // narrowed: state.value is in scope here
case "failed":
return `失敗(${state.error})`; // narrowed: state.error is in scope here
default: {
const _exhaustive: never = state;
throw new Error(`unhandled state: ${JSON.stringify(_exhaustive)}`);
}
}
}
Add a variant { kind: "cancelled"; at: number } to the union and state in default is no longer never — const _exhaustive: never = state fails to compile, pointing at this switch. Add the case, the build goes green. The default also guards malformed wire data at runtime.
If branches are pure constant mappings AND no branch needs per-variant fields, a satisfies Record<...> lookup is also exhaustive and fine:
const LABEL = {
idle: "待機中",
loading: "読み込み中",
} satisfies Record<FetchState["kind"], string>;
The moment any branch needs a per-variant field (state.value, state.error), go back to switch — the map can't reach those fields.
| Anti-pattern | Why it fails |
|---|---|
if (s.kind === "x") … if (s.kind === "y") … + trailing return fallback | The fallback silently swallows a new variant — no compile error. if-chains give no exhaustiveness. |
assertNever(state) helper in default | A thin wrapper — banned here (薄い wrapper 関数は作らない). Inline the const _exhaustive: never = state instead. |
Record map when a branch needs a per-variant field | The map throws away discriminant narrowing; you lose access to the variant's fields. |
default: return "" / default: return fallback | Defeats the whole point — a forgotten variant compiles and ships. |
default that returns a fallback "to be safe" — it disables exhaustiveness. The default must assign to never.assertNever (or exhaustiveCheck, absurd) helpers. Inline the never-assignment; do not add a wrapper.precise-type-modeling), then switch.if (state.kind === …) for a union → use switch.assertNever → inline const _exhaustive: never = state.default: that returns a value → only never-assignment belongs there.return fallback after the branches → a new variant will be swallowed silently.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
8 skills in this plugin
/chaining-neverthrow-resultsUse when composing or chaining neverthrow Result / ResultAsync values — sequencing multi-step operations where each step can fail, recovering from a specific error, or deciding where to turn a Result into a plain value or HTTP response. Also when reaching for .match mid-pipeline, _unsafeUnwrap, isErr()+.value unwrapping, safeTry / yield*, or a try/catch around Result-returning code./explicit-primitive-conversionUse when converting a value to a number, string, or boolean in TypeScript, or annotating a primitive type — about to write Number(x), Number.parseInt, Number.parseFloat, String(x), Boolean(x), unary +x, !!x, .filter(Boolean), .map(Number), or a : Number / : String / : Boolean type annotation./modeling-errors-as-classesUse when defining an error type, an error channel for a Result/ResultAsync, or a failure that crosses a throw / Promise rejection / serialization boundary — anything you would otherwise model as an object union like `{ name: 'XError'; message: string }`./precise-type-modelingUse when authoring or converting TypeScript — defining a type / interface / function signature, typing arguments or state, deciding between optional fields and a union, or about to reach for any, unknown, Record<string, unknown>, an index signature, or an as cast. The minimum modeling rules for making code TypeScript./prefix-match-processorUse when branching keys off the prefix of a string (startsWith, namespaced name families like "file.", "edit.", "edit.image.") and the number of prefix branches is growing, when a prefix if-chain is becoming hard to test or extend per-family, or when adding several new prefix families to an existing dispatcher./switch-patternConvert `if`-based discriminated-value matching (HTTP status codes, event/action `type` fields, mode strings) to exhaustive `switch` statements. Use when refactoring existing code to the project style or when reviewing a file for `if (x === ...) / if (x !== ...)` clusters that should be flattened to switch.
/typescript-modeling:branching-modeled-state-with-switchfrom typescript-modeling
npx claudepluginhub naporin0624/astro-agent-starter --plugin typescript-modeling