Your switch forgets a case. Exhaustiveness checking catches it.
How to make the TypeScript compiler fail the build when a switch statement doesn't handle every member of a union — using the `never` type.

Someone on your team adds "refunded" to the OrderStatus union. Three files use that type. Two of them get updated. The third — the one that renders the status badge on the order page — doesn't.
It compiles. It ships. A refunded order sits on a customer's dashboard labeled "Processing" for two weeks, because the switch that renders the badge has no idea "refunded" exists, falls through to nothing, and renders an empty string.
Nobody wrote a bug. Someone just didn't update every place that needed updating — which is the normal, forgivable thing that happens on every team, every week. The question is whether your type system notices.
The instinctive move is a default case:
This feels safe. It isn't. Add "refunded" to the union tomorrow, and this function keeps compiling, keeps running, and keeps quietly mislabeling refunded orders as "Processing" — the exact bug from the top of this post, except now it's load-bearing, because the default case was designed to swallow anything unexpected.
A console.warn in the default branch helps you find out after it's in production, if anyone's watching logs. What you actually want is to find out before you hit "merge."
TypeScript already tracks, branch by branch, which members of a union a switch has ruled out. Inside the default case of a fully-handled switch, the type of status isn't OrderStatus anymore — it's narrowed down to whatever's left over. If every case was handled, what's left over is nothing at all: the never type, TypeScript's name for "a value that cannot exist."
That's the lever. Write a function that only accepts never, and call it from the default branch:
Right now this compiles cleanly, because TypeScript has proven status really is never by the time it reaches default — every other member of the union got its own case above.
Now add "refunded" to OrderStatus and change nothing else:
The badgeText function no longer compiles. TypeScript narrows status in the default branch to "refunded" — one real, leftover case — and "refunded" is not assignable to the parameter type never. The error points at the exact line, in the exact file, that forgot to handle the new status. Not a runtime log line three weeks from now. A red squiggle before you push.
never is TypeScript's type for "this code path cannot be reached" — functions that always throw, infinite loops, and branches a union's structure has already ruled out. Assigning a value to a never-typed parameter is only legal if TypeScript can prove no value is left to assign. That's precisely what "the switch handled every case" means. You're not writing a special exhaustiveness feature; you're pointing a general-purpose type at a question it's already equipped to answer.
It composes the same way with if/else if chains — narrowing works the same way there:
Same guarantee, same failure mode when you add "refunded" without touching this function.
This catches missing cases, not wrong ones. If you handle "refunded" by copy-pasting the "paid" branch, the compiler has nothing to say — that's a logic bug, not an exhaustiveness bug, and no type system catches those for free.
It also needs an honest default (or a final else) with nothing after it that would make the branch unreachable another way — wrap the call in a function typed to return the right thing, as above, rather than leaving it as a bare statement the compiler could route around. And it only protects the functions you've actually wired this way; it doesn't retroactively audit a codebase. The value compounds the way tests do — add it to every switch over a union that models real states (order status, payment status, permission level, wizard step), and the moment someone extends that union, the compiler goes and finds every place that forgot, for you, before a reviewer has to.
Runs right in your browser — poke at it and watch the concept react live.
Go check whatever OrderStatus-shaped type your codebase already has — a job status, a subscription tier, a form step — and find the switch statements over it. If none of them end in a call to a never-typed function, the next person to extend that union is one keystroke away from shipping the same quiet gap "refunded" fell into above. One function, reused everywhere: that's the whole fix.
Think it clicked? Take the 8-question quiz →
Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.
Has a missing switch case ever gotten past your reviewers and into production? What did it look like when you finally found it?
- My Reducer's Switch Stopped Being Exhaustive.
as constFixed It. - Your TypeScript config objects are losing type information.
satisfiesfixes it. - Stop Formatting Currency With Regex.
Intl.NumberFormatDoes It.
🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.
Thanks for reading! Let's stay connected:
- ⭐ GitHub — follow me and star the projects: github.com/parsajiravand
- 💬 Discord — join the frontend best-practices community: discord.gg/d9KRhuAwQ
- 📸 Instagram — frontend best practices, daily: @bestpractice___
Keep reading
One post a day, in your inbox
Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.
0 comments
Sign in to join the discussion, like comments, and save articles for later.