never
never is the type of a value that cannot exist. A function that always throws or loops forever returns never. A union member that has been fully narrowed away becomes never.
Detailed Explanation
Think of a type as a set of values. string is a huge set. "ready" is a set of one. never is the empty set. There is no runtime value you can produce that has type never, because there is no such value.
That sounds useless until you see the two jobs it does.
The first job is describing control flow that does not continue. function fail(message: string): never { throw new Error(message); } never returns. After a call to fail, the compiler treats the rest of the block as unreachable. The same is true of a function whose body is while (true) {}. Annotating these as never is documentation the compiler enforces: if you later add a return statement, the annotation fails.
The second job is what remains after narrowing removes every member of a union. That leftover is never, and assigning it to a helper is how exhaustiveness checking works. If you add a new union member and forget a switch case, the leftover is no longer never and the helper errors.
never is the bottom type. It is assignable to every other type, because the empty set is a subset of every set. The reverse is not true: nothing except never itself is assignable to never. That is why const x: never = 1 is an error, and why a function declared to return never cannot return a number.
never also disappears from unions. string | never is just string. That is why filtering a union with a conditional type uses never as the reject branch: T extends string ? T : never keeps the string members and drops the rest.
A common confusion is mixing never with void and undefined. void means "the caller must ignore the return value." undefined is a real value. never means there is no return at all. A function that returns undefined on purpose should be typed undefined or void, not never.
You will also meet never as a symptom. An intersection of incompatible types (string & number) collapses to never. A property conflict in an intersection ({ id: string } & { id: number }) makes that property never. When a type unexpectedly becomes never, you usually have an over-constrained generic or a bad intersection, not a missing value.
Code Examples
1. A function that cannot return.
// 1. A function that cannot return.
function fail(message: string): never {
throw new Error(message);
}
function first(items: string[]): string {
if (items.length === 0) {
fail("empty list");
}
return items[0]; // after fail(), this line is still reachable only when length > 0
}2. never as the leftover of a fully narrowed union.
// 2. never as the leftover of a fully narrowed union.
type Status = "idle" | "loading" | "done";
function assertNever(value: never): never {
throw new Error(`unexpected: ${String(value)}`);
}
function label(status: Status): string {
switch (status) {
case "idle":
return "Idle";
case "loading":
return "Loading";
case "done":
return "Done";
default:
return assertNever(status); // status is never here if every case is covered
}
}3. Filtering a union with never, and a collapsed intersection.
// 3. Filtering a union with never, and a collapsed intersection.
type KeepStrings<T> = T extends string ? T : never;
type OnlyText = KeepStrings<string | number | boolean>; // string
type Impossible = string & number; // never
type Conflict = { id: string } & { id: number };
declare const row: Conflict;
row.id; // never — the two id types have no overlapCommon Gotchas
Returning a value from a `never` function is a type error
if the body can complete normally, the annotation is a lie. Use `void` or a real return type instead.
A type that became `never` is usually over-constrained
look for `T extends X` plus a second constraint that contradicts `X`, or an intersection of incompatible property types.
`never` vanishes from unions and absorbs intersections
`A | never` is `A`. `A & never` is `never`. That is why conditional filters use `never` as the drop branch.
`never[]` is not an empty array you can push into
the empty array literal `[]` is often inferred as `never[]` until you annotate it. Pushing a string then fails. Write `const items: string[] = []`.