Sub topic 12
How `never` Behaves in Unions, Intersections, and Conditional Types
Assume you know basic TypeScript type annotations, interfaces, union types, and conditional types. You should also be able to run tsc or read type errors in an editor.
never is the type with no possible values. A variable of type never cannot receive a value. A function that always throws or never finishes can return never:
function fail(message: string): never {
throw new Error(message);
}The compiler uses never to represent impossible states. Its behavior becomes especially useful in unions, intersections, and conditional types.
`never` in unions
A union means “one of these possible types.” Since never has no values, adding it does not add any possibilities.
type TextOrNothing = string | never;
// Equivalent to: string
type Status = "ready" | "running" | never;
// Equivalent to: "ready" | "running"This rule lets TypeScript remove members from unions with conditional types. A conditional type has the form T extends U ? A : B. It chooses A when T is assignable to U; otherwise, it chooses B.
When the checked type is a type parameter, conditional types distribute over unions. TypeScript evaluates each union member separately:
type Without<T, U> = T extends U ? never : T;
type Visible = Without<"public" | "private" | "internal", "private">;
// "public" | "internal"The compiler evaluates the type like this:
"public" extends "private" ? never : "public" => "public"
"private" extends "private" ? never : "private" => never
"internal" extends "private" ? never : "internal" => "internal"The result is "public" | never | "internal". Because never disappears from a union, the final result is "public" | "internal".
`never` in intersections
An intersection means “a value that satisfies all of these types.” If two types have incompatible requirements, their intersection can have no values.
type Impossible = string & number;
// neverA value cannot be both a JavaScript string and a JavaScript number, so the intersection is impossible.
Object intersections can also become impossible when the same property has incompatible types:
type Conflicting = {
id: string;
} & {
id: number;
};
// neverSome intersections remain useful because their requirements are compatible:
type Named = { name: string };
type Aged = { age: number };
type Person = Named & Aged;
// { name: string; age: number }| Type expression | Result | Reason | Column |
|---|---|---|---|
| `string \ | never` | `string` | `never` adds no values |
| `string & number` | `never` | No value satisfies both types | |
| `{ name: string } & { age: number }` | Both properties | The requirements are compatible |
Conditional types and detecting `never`
A conditional type distributes only when a naked type parameter appears on the left side of extends. This creates a common surprise:
type IsNever<T> = T extends never ? true : false;
type Result = IsNever<never>;
// never, not trueBecause never represents an empty union, there are no members to test. Wrap the type parameter in a tuple to prevent distribution:
type IsNever<T> = [T] extends [never] ? true : false;
type A = IsNever<never>; // true
type B = IsNever<string>; // falseThe tuple changes the test from a distributive conditional to one comparison of the entire type.
>callout note >Use never to remove union members deliberately. Use tuple wrapping, such as [T] extends [never], when you need to test whether a type is exactly never. >```
Practical use
A filtering utility can use never to keep only selected members:
type OnlyStrings<T> = T extends string ? T : never;
type Names = OnlyStrings<string | number | boolean>;
// stringThe non-string members become never, then disappear from the resulting union. This pattern forms the basis of many built-in and custom utility types.
Exercise
What are the resulting types of type A = (1 | 2 | 3) & (2 | 3) and type B = Exclude<"a" | "b", "a">?
Show answer
A is 2 | 3, because those values satisfy both unions. B is "b", because the matching "a" member becomes never and is removed from the union.