never type
Represents values that never occur, such as unreachable code paths.
Syntax
never Examples
function fail(msg: string): never {
throw new Error(msg);
}
function exhaustive(x: never): never {
throw new Error('unhandled: ' + x);
}
type Shape = { kind: 'a' } | { kind: 'b' };
function area(s: Shape) {
switch (s.kind) {
case 'a': return 1;
case 'b': return 2;
default: return exhaustive(s);
}
}
Notes
`never` is the type of functions that never return (throw or infinite loop)
and of impossible values. It is the bottom type, assignable to every type but
with no assignable values. Empty unions reduce to `never`; use it for
exhaustiveness checks in switches.