Exhaustive switches & never checks
Write exhaustive unions with switch and catch missing cases using never; build a safe assertNever helper.
Exhaustive switches & never checks is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Make exhaustive switches over unions and use never to catch missing cases at compile-time.
Exhaustive switch
Discriminated unions use a common kind field. A switch on that field narrows each case safely.
type Circle = { kind: "circle"; radius: number }
type Square = { kind: "square"; size: number }
type Shape = Circle | Square
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius * s.radius
case "square":
return s.size * s.size
}
}When unions grow
When a new member (e.g. triangle) is added, we will catch it with never so that the skipped case does not silently generate a bug.
type Triangle = { kind: "triangle"; base: number; height: number }
// If we extend Shape but forget to add case, we want a compile-time error
// type Shape = Circle | Square | TriangleassertNever
The assertNever parameter must be of type never, so that if a new variant falls into the default, it will give a compile error.
function assertNever(x: never): never {
throw new Error(`Unhandled case: ${String(x)}`)
}
function areaSafe(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius * s.radius
case "square":
return s.size * s.size
default:
return assertNever(s) // if Shape grows, compiler errors here
}
}Literal unions
The same pattern applies to string literal unions: cover every possible value, call assertNever on the default.
type Status = "idle" | "loading" | "success" | "error"
function toIcon(s: Status) {
switch (s) {
case "idle": return "⏸"
case "loading": return "⏳"
case "success": return "✅"
case "error": return "⚠️"
default: return assertNever(s)
}
}
// Alternative: const check
const _check: never = ((): Status => "idle")() as never // force compile-time check with careTips
Tips: Turn on noFallthroughCasesInSwitch; prefer discriminated unions; default is only for assertNever.
// Tips
// - Enable noFallthroughCasesInSwitch in tsconfig for safer switches
// - Prefer discriminated unions over boolean flags
// - Keep default branch only to call assertNever; avoid swallowing casesassertNever check
Quick check: What is the purpose of assertNever in an exhaustive switch?
Recap
Recap: Discriminated unions + exhaustive switch = safer code. never and assertNever catch missed cases in compilation.
Frequently asked questions
Is the “Exhaustive switches & never checks” lesson free?
Yes — the full text of “Exhaustive switches & never checks” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Exhaustive switches & never checks”?
Write exhaustive unions with switch and catch missing cases using never; build a safe assertNever helper. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Exhaustive switches & never checks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Exhaustive switches & never checks
- Predicate functions & satisfies operator
- Refining unions across function boundaries