functionthrowAppError(msg: string): never {thrownewError(msg);}// Used for exhaustive checkstypeShape="circle"|"square";functiongetArea(shape:Shape): number {switch(shape){case"circle":returnMath.PI*10;case"square":return100;default:const _exhaustive: never = shape;return _exhaustive;}}
Output
// never means this code path should be unreachable
Note never represents values that never occur. A function returning never must not return normally (throw or infinite loop). Assigning to never in a default branch ensures you handle every union member - adding a new Shape member causes a compile error.
functionassertNever(value: never): never {thrownewError(`Unexpected: ${value}`);}
Example
typePaymentMethod="card"|"bank"|"crypto"|"paypal";functionprocessPayment(method:PaymentMethod): string {switch(method){case"card":return"Processing card payment";case"bank":return"Processing bank transfer";case"crypto":return"Processing crypto payment";case"paypal":return"Processing PayPal";default:// If a new member is added to PaymentMethod,// this line causes a compile errorconst _exhaustive: never = method;thrownewError(`Unknown method: ${_exhaustive}`);}}
Output
// Adding a fifth payment method causes a compile error at the never assignment
Note The never trick ensures you handle every union member. When a new member is added, TypeScript sees it is not handled and cannot assign it to never. This is critical for state machines, reducers, and any logic that must cover all cases. Some teams extract this into a shared assertNever utility function.
Frequently asked questions
How does TypeScript handle exhaustive check?
TypeScript covers this with 2 copy-ready snippets on this page. The "never" snippet in TypeScript uses `function varName(): never { ... }`.
Which code does the TypeScript example use?
The "never" snippet uses `function varName(): never { ... }`, from the Basic Types section of the TypeScript cheat sheet.
What other TypeScript snippets are shown for "exhaustive check"?
Besides "never", this page also shows "Exhaustive Switch / If-Else".
Is there anything to watch out for?
Yes. For "never": never represents values that never occur. A function returning never must not return normally (throw or infinite loop). Assigning to never in a default branch ensures you handle every union member - adding a new Shape member causes a compile error.