Lesson 24 — Exhaustive checks with never
Make the compiler find missing cases
never is the type of a value that cannot exist. In a switch over a discriminated union, once every case is handled, the leftover value in default has type never. If someone adds a new shape and forgets a case, assigning it to a never variable becomes a compile error that points straight at the gap.
default: {
const unreachable: never = s; // error if a case is missing
throw new Error("Unhandled");
}
Why it matters: the compiler, not your users, tells you about the case you forgot.
A triangle shape was added. Handle case "t" (base * h / 2) so the never check passes. Expect 16, then 6.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based TypeScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for TypeScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →