Never represents an impossible value. An exhaustive switch can expose newly added union members as compile errors.
Before you start
You should be comfortable with JavaScript values, functions and objects. Use a TypeScript project with strict checking enabled when trying the examples. Separate what the compiler proves from what must still be checked when values arrive at runtime.
The practical goal is to reason through this situation: After handling every result kind, assign the remaining value to never. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Define the full union
A closed union lists every supported action or result. Exhaustiveness is meaningful only if the compiler knows those possibilities.
Step 2: Handle each member
Use a switch on the discriminant and return from each branch. Fallthrough would leave the supposedly completed state reachable.
Step 3: Assert the remainder impossible
Pass the remaining value to a function accepting never. Adding another member then creates a compile error at the missing consumer.
Worked scenario
After handling every result kind, assign the remaining value to never.
function unreachable(value: never): never {
throw new Error('Unhandled result');
}
type Status = 'ready' | 'failed';
function message(status: Status): string {
switch (status) {
case 'ready': return 'Continue';
case 'failed': return 'Retry';
default: return unreachable(status);
}
}Add 'cancelled' to Status without changing message. The default branch now receives a possible string literal rather than never; the error identifies an incomplete implementation.
Common mistake
Default branches returning a fallback can hide a forgotten case.
Verify the behavior
Add and remove a union member. Confirm the check fails only while a supported case is unhandled. Never alone does not validate values obtained from untyped callers.
Interview exercise
What happens when a new kind is added?
Answer and reasoning
The never assignment fails until the new case is handled, making evolution visible to consumers.
Continue learning
Compare the scenario with the TypeScript interview questions and test your understanding with the TypeScript MCQs. For terminology and implementation details, consult the reference material.