Exhaustiveness with never (intro)
Use never to enforce exhaustive switches on unions and catch missing cases early.
Exhaustiveness with never (intro) is a free TypeScript Academy lesson on CoddyKit — lesson 3 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: Ensure your switch handles all members of a union. The never type helps the compiler alert you when a new case is missing.
Union setup
Create a discriminated union with a literal tag (e.g., kind) to switch on later.
type Circle = { kind: "circle"; radius: number };
type Square = { kind: "square"; size: number };
type Shape = Circle | Square;Non-exhaustive switch
A missing branch compiles, but logic is incomplete. We need a way to force a compile-time failure when a case is missing.
function areaBad(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius ** 2;
// OOPS: forgot "square" -> no compile error yet
}
return 0; // silently wrong
}
console.log(areaBad({ kind: "circle", radius: 2 }));assertNever helper
Define assertNever: it accepts only never. If a value reaches it, the switch was not exhaustive and the compiler will flag it.
function assertNever(x: never): never {
throw new Error("Unhandled case: " + JSON.stringify(x));
}Exhaustive switch
Add a default that calls assertNever. When a new union member appears, the compiler forces you to handle it.
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius ** 2;
case "square":
return s.size * s.size;
default:
return assertNever(s); // compiler error if a member is unhandled
}
}
console.log(area({ kind: "square", size: 3 }));Guidelines
Guidelines:
- Use a discriminant (like
kind) on union members. - Keep the default + assertNever pattern.
- Prefer clear return types so missing cases are obvious.
Exhaustiveness check
Quick check: How do you make a switch over a discriminated union exhaustive?
Recap
Recap: never indicates an impossible value. Use assertNever in the default branch to force complete, future-proof switches.
Frequently asked questions
Is the “Exhaustiveness with never (intro)” lesson free?
Yes — the full text of “Exhaustiveness with never (intro)” 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 “Exhaustiveness with never (intro)”?
Use never to enforce exhaustive switches on unions and catch missing cases early. 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 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Exhaustiveness with never (intro)” 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
- typeof, equality, truthiness narrowing
- in, instanceof, discriminated unions
- Exhaustiveness with never (intro)