Distributive Conditional Types
Control how conditionals distribute over unions.
A Surprising Behavior
Conditional types have one special rule: when the checked type is a naked type parameter and you pass a union, the conditional distributes over each member separately, then unions the results.
This is the foundation of Exclude, Extract, and many utilities.
type ToArray<T> = T extends unknown ? T[] : never;
type A = ToArray<string | number>;
// string[] | number[] (NOT (string | number)[])What "Naked" Means
"Naked" means the type parameter appears by itself on the left of extends, not wrapped in another type. Distribution only happens for naked parameters.
type Naked<T> = T extends string ? "y" : "n";
// T is naked -> distributes over unionsAll lessons in this course
- Types as a Computation Language
- Type-Level Conditionals
- Type-Level Recursion
- Distributive Conditional Types