What Is the never Type in TypeScript?
Understand the TypeScript never type: how it differs from void, exhaustiveness checks in switch statements, and real code examples for interviews.
Expected Interview Answer
The never type in TypeScript represents values that can never occur — it is assigned to expressions that always throw, never return, or to a type position the compiler has proven is unreachable through exhaustive narrowing.
Unlike void, which means a function returns no useful value but still completes normally, never means the function never completes at all, either because it throws, loops forever, or calls process.exit. The type also appears when narrowing exhausts every member of a union, leaving an empty set of possibilities. This makes never the natural return type for assertion functions and the backbone of exhaustiveness checks in switch statements, where assigning the remaining variable to a parameter typed never causes a compile error if a new union member is ever added without a matching case.
- Marks functions that always throw or never return
- Enables compile-time exhaustiveness checks on unions
- Acts as the empty bottom type in the type hierarchy
- Catches missing switch cases when a union grows
- Signals unreachable code paths to readers and tooling
AI Mentor Explanation
The never type is like a delivery so unplayable it is ruled a wide before the batter even reacts — an outcome that officially cannot count as a normal ball faced. Umpires use this the same way TypeScript uses never: to formally mark a branch of play that the rulebook says will not occur, so the scorer never has to record a result for it.
Step-by-Step Explanation
Step 1
Bottom type
never sits at the bottom of the type hierarchy — it is assignable to every other type, but no value except one typed never can be assigned to it.
Step 2
Non-returning functions
Functions that always throw, infinite-loop, or call process.exit are inferred to return never rather than void.
Step 3
Union narrowing to empty
When a switch or if-chain has handled every member of a union, the remaining type in the else branch narrows to never.
Step 4
Exhaustiveness checks
Assigning the narrowed variable to a parameter typed never in a default case forces a compile error if a new union member is added without handling it.
Step 5
Distinguish from void
void means 'returns, but the value is not useful'; never means 'does not return at all' — mixing the two hides real bugs.
What Interviewer Expects
- Distinguishes never from void clearly
- Knows never is the empty/bottom type in the hierarchy
- Can implement an exhaustiveness check using never
- Explains why narrowing a fully-handled union yields never
- Names real use cases: assertion functions, unreachable branches
Common Mistakes
- Confusing never with void as if they were interchangeable
- Believing never means 'any value is allowed here'
- Forgetting to use never for exhaustiveness checks in switch statements
- Assuming a function returning never can still fall through to a caller
Best Answer (HR Friendly)
“The never type tells the compiler that a piece of code can never actually produce a value — either because a function always throws an error, or because every possible case in a decision has already been handled. Developers use it to catch mistakes early, for example making sure that if a new option is ever added to a list of choices, the code fails to compile until someone handles it.”
Code Example
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; side: number };
function area(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2;
case 'square':
return shape.side ** 2;
default:
// If a new Shape variant is added and not handled above,
// `shape` will no longer be `never` here, and this line fails to compile.
const exhaustive: never = shape;
throw new Error(`Unhandled shape: ${exhaustive}`);
}
}
function fail(message: string): never {
throw new Error(message); // return type is inferred as never
}Follow-up Questions
- How is never different from void and undefined?
- How do you use never to build an exhaustiveness check?
- Can never appear as a function parameter type usefully?
- What happens if you try to assign a string to a variable typed never?
- How does never interact with union types during narrowing?
MCQ Practice
1. Which best describes the never type?
never represents values that logically cannot occur, unlike void which just means no meaningful return value.
2. What does a switch's default branch narrow an already-exhausted union to?
Once every union member has a matching case, TypeScript narrows the remaining type in the default branch to never.
3. What return type does TypeScript infer for a function that always throws?
A function that always throws (and never returns normally) is inferred to have a return type of never.
Flash Cards
How is never different from void? — void means a function returns, just with no useful value; never means the function does not return at all.
What is never used for in a switch statement? — Assigning the narrowed remainder to a never-typed variable in default creates a compile error if a new case is added but unhandled.
Where does never sit in the type hierarchy? — At the bottom — it is assignable to every type, but only a never-typed value can be assigned to it.
What return type do functions that always throw get? — never, since they never complete normally.