Asserts that a code path is unreachable; throws if executed.
Use for exhaustiveness checking in switch statements. TypeScript will error if all cases aren't handled.
Should be never if all cases are handled.
never
Always throws with the unexpected value.
type Fruit = 'apple' | 'banana';function getColor(fruit: Fruit): string { switch (fruit) { case 'apple': return 'red'; case 'banana': return 'yellow'; } return unreachable(fruit); // Compile error if case missing} Copy
type Fruit = 'apple' | 'banana';function getColor(fruit: Fruit): string { switch (fruit) { case 'apple': return 'red'; case 'banana': return 'yellow'; } return unreachable(fruit); // Compile error if case missing}
Asserts that a code path is unreachable; throws if executed.
Use for exhaustiveness checking in switch statements. TypeScript will error if all cases aren't handled.