Convert `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.
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, though model invocation is off: only you can start it.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Refactor if-based pattern matching on a single discriminated value (HTTP status, event type, mode) to switch blocks. Backs the project rule documented in .agents/rules/early-return.md (Switch for Discriminated Unions).
Apply switch refactor when all of the following hold:
if (x === ...) / if (x !== ...) checks on that same value.Skip switch refactor when:
if (x !== null) return null;) — keep the guard clause.if (x === 1 && y === 2)) — switch can't express AND across discriminators.if (count > 0)).if (res.status === <success>) with no else branch.
// Before
if (res.status === 200) {
const data = await res.json();
set(data.entries);
}
// After
switch (res.status) {
case 200: {
const data = await res.json();
set(data.entries);
break;
}
}
if (res.status !== <success>) { log } is an inverted check. Flip to switch with early-return success and default for log.
// Before
if (res.status !== 201) {
const data = await res.json();
console.error('[submitEntry]', res.status, data);
}
// After
switch (res.status) {
case 201:
return;
default: {
const data = await res.json();
console.error('[submitEntry]', res.status, data);
}
}
When the failure path throws.
switch (res.status) {
case 200: {
const data = await res.json();
return data.entries;
}
default:
throw new Error(`Failed to fetch entries: ${res.status}`);
}
type)Already standard for event handlers and worker protocols.
switch (event.type) {
case 'progress':
timeline.progress(event.value);
break;
case 'reset':
timeline.progress(0);
break;
// ...
}
case with { } when declaring const / await. Avoids fall-through declaration leaks.break; after side-effect cases, return …; when the case is terminal.default: last. Use it for "everything else" (log, throw, fallback).case 200: break; solely to silence default — instead invert and use default for the cold path (Pattern B).case fall-through unless intentional and commented.eslint-disable for switch unless the rule conflicts (the project's lint already permits this style).When the user invokes /switch-pattern [glob]:
$ARGUMENTS is empty, ask via Grep which files contain if (.*\.status\s*[!=]==. Otherwise glob the argument.if-on-discriminator clusters.eslint-disable comments (re-attach to the new case body)pnpm lint && pnpm typecheck. Report pass/fail. If failures introduced, revert and report.<file>: N if-clusters → switch (Patterns A=x, B=y, ...).// ❌ Inverted-only with no else — Pattern B candidate
if (res.status !== 200) {
console.error(...);
}
// (success path is implicit — readers must reason about the negation)
// ❌ Chained if/else on the same field — switch is shorter & exhaustive
if (status === 'idle') return null;
else if (status === 'loading') return <Spinner />;
else if (status === 'ready') return <Data />;
else return <Error />;
// ❌ Nested if where switch + default would be flat
if (res.ok) {
if (res.status === 201) doCreate();
else if (res.status === 200) doUpdate();
}
This skill does not:
const _exhaustive: never = x default (no assertNever helper).match-style libraries (ts-pattern, neverthrow .match) — those are intentional pattern syntax.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
/typescript-modeling:switch-pattern [file-or-directory-glob]from typescript-modeling
npx claudepluginhub naporin0624/astro-agent-starter --plugin typescript-modeling8 skills in this plugin
/branching-modeled-state-with-switchUse 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./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.