Distributive Conditional Types in TypeScript
Understand distributive conditional types in TypeScript, see why unions and never behave unexpectedly, and use tuple wrappers to control distribution.
A distributive conditional type is a conditional type whose checked type is a bare type parameter. When you pass a union to that parameter, TypeScript evaluates the conditional once for each member of the union and joins the results into a new union.
You write a small type-level check, pass it string | number, and expect false. TypeScript shows boolean. Or you pass never and get back never, which is neither branch you wrote.
This article skips the basics of T extends U ? X : Y (Understanding infer in TypeScript covers those). It goes straight to the distribution rule: how to turn it off, the never trap, and how to tell which behavior a given type has.
Key Takeaways
- A conditional type distributes only when the type on the left of
extendsis a bare type parameter and the argument is a union. IsString<string | number>resolves tobooleanbecause TypeScript evaluates it asIsString<string> | IsString<number>.- Writing
[T] extends [U], with both sides wrapped in a one-element tuple, turns distribution off. - A distributive conditional type given
neverreturnsnever, becauseneveris the empty union and there are no members to evaluate. ExcludeandExtractare distributive. Since TypeScript 4.8,NonNullable<T>is defined asT & {}and is not a conditional type.
Why Does a Union Check Return boolean Instead of false?
A distributive conditional type returns boolean for a mixed union because TypeScript checks each union member separately. The surprise comes from a plain-looking check:
type IsString<T> = T extends string ? true : false;
type A = IsString<string | number>; // => boolean
Read as English, the question is whether string | number is a string. It isn’t, so most developers expect false. TypeScript answers a different question: it asks, for each member of the union, whether that member is a string.
How Do Distributive Conditional Types Work?
A conditional type distributes when the type being checked, on the left of extends, is a bare type parameter and the argument for that parameter is a union. TypeScript evaluates the conditional once for each member and unions the results. The TypeScript handbook describes this behavior using a ToArray<string | number> example, which resolves to string[] | number[].
Applied to IsString, the expansion looks like this:
type A = IsString<string | number>;
// = IsString<string> | IsString<number>
// = true | false
// = boolean
Two details follow from the rule. First, only the checked side distributes. The type on the right of extends is always compared as a single, whole type:
type HasString<T> = string extends T ? true : false;
type B = HasString<string | number>; // => true
If the right side distributed, this would produce boolean. Second, boolean is the union true | false, so a distributive conditional type splits it into two checks just like any other union:
type Flip<T> = T extends true ? false : true;
type C = Flip<boolean>; // => boolean (false | true)
Built-In Utility Types That Rely on Distribution
Exclude<T, U> and Extract<T, U> are distributive conditional types. They walk a union one member at a time and keep the members that are not assignable to U (for Exclude) or the members that are (for Extract). The standard library defines them as T extends U ? never : T and T extends U ? T : never. Because a union absorbs never, the members that hit the never branch disappear from the result. The Utility Types reference lists both.
type D = Exclude<"a" | "b" | "c", "a">; // => "b" | "c"
type E = Extract<string | number | (() => void), Function>; // => () => void
type F = NonNullable<string | null | undefined>; // => string
NonNullable gives the result you would expect from a distributive filter, but it no longer is one. Since TypeScript 4.8, NonNullable<T> is defined as T & {}. It still removes null and undefined from a union, but it does so by intersection rather than by distributing a conditional type.
Switching Distribution Off With [T] extends [U]
Writing [T] extends [U], with both sides wrapped in a one-element tuple, turns distribution off, so TypeScript checks the union as one type. The handbook gives this bracket syntax as the way to avoid distribution:
type IsStringStrict<T> = [T] extends [string] ? true : false;
type G1 = IsString<string | number>; // => boolean
type G2 = IsStringStrict<string | number>; // => false
Any wrapper that leaves T no longer bare has the same effect, whether it is a tuple, a function type or an object type. The one-element tuple is the convention because it reads clearly and adds no other meaning. Wrap both sides. [T] extends string asks whether a tuple is a string, and the answer is always false.
Why Does a Distributive Conditional Type Return never?
A distributive conditional type given never returns never instead of either branch. never is the empty union, so there are no members to evaluate, and mapping over nothing produces the empty union. This is why a naive IsNever fails:
type IsNeverNaive<T> = T extends never ? true : false;
type H1 = IsNeverNaive<never>; // => never
type H2 = IsNeverNaive<string>; // => false
The fix is the same bracket opt-out. [never] is a real one-element tuple, so TypeScript runs the check:
type IsNever<T> = [T] extends [never] ? true : false;
type I1 = IsNever<never>; // => true
type I2 = IsNever<string>; // => false
When Distribution Helps and When It Bites
Distribution helps when each union member needs its own result. A common case is mapping event names to handler types:
interface AppEvents {
click: { x: number; y: number };
keydown: { key: string };
}
type Handler<E extends keyof AppEvents> =
E extends keyof AppEvents ? (payload: AppEvents[E]) => void : never;
type HandlerStrict<E extends keyof AppEvents> =
[E] extends [keyof AppEvents] ? (payload: AppEvents[E]) => void : never;
type J1 = Handler<"click" | "keydown">;
// => ((payload: { x: number; y: number; }) => void) | ((payload: { key: string; }) => void)
type J2 = HandlerStrict<"click" | "keydown">;
// => (payload: { x: number; y: number; } | { key: string; }) => void
Handler keeps each event paired with its own payload. HandlerStrict loses that pairing: it gives one function whose payload could be either shape, so a click handler type now admits { key: string }. A typed API layer usually wants the distributive version.
Distribution bites when the question concerns the whole union:
type AllStrings<T> = T extends string ? true : false;
type AllStringsStrict<T> = [T] extends [string] ? true : false;
type K1 = AllStrings<"a" | 1>; // => boolean
type K2 = AllStringsStrict<"a" | 1>; // => false
In a form library, a check like AllStrings that is meant to reject a mixed field type returns boolean instead. Any downstream branch that tests the result against true or false then misbehaves. The bracketed form answers the question that was asked.
How to Tell Which Behavior You Have
A conditional type distributes only if its type parameter appears unwrapped on the left of extends. If a conditional type returns an unexpected union, or returns never when you passed never, check that first.
| Form | Distributes? |
|---|---|
T extends … | Yes |
[T] extends […] | No |
(string | number) extends string | No, because there is no type parameter |
Conclusion
Distribution is a rule about syntax, not about intent. A bare type parameter on the checked side splits unions and swallows never, and wrapping that parameter in a tuple turns both behaviors off. When a conditional type returns something unexpected, look at the left of extends first, then decide whether you want one answer per member or one answer for the whole union. For more type-level patterns, see Complex Typing in TypeScript.
FAQs
What does a distributive conditional type return when you pass any or unknown?
Passing any to a distributive conditional type returns the union of both branches, so IsString<any> resolves to boolean. The exception is when the type on the right of extends is itself any. The unknown type is not a union and does not split. It is checked as one type and usually takes the false branch, so IsString<unknown> resolves to false.
Does adding a constraint to the type parameter stop distribution?
No. A constraint such as T extends string | number in the parameter list only limits which arguments are allowed. Distribution depends on whether T appears bare on the left of extends inside the conditional. A constrained parameter used bare still splits a union argument member by member. Only wrapping it, for example as [T] extends [U], switches distribution off.
Does ReturnType distribute over a union of function types?
Yes. ReturnType is defined as a conditional type that checks the bare parameter T against a function signature and uses infer to capture the return type, so it distributes. ReturnType<(() => string) | (() => number)> evaluates each function separately and resolves to string | number. Parameters and InstanceType follow the same pattern and also distribute over union arguments.
Complete picture for complete understanding
Capture every clue your frontend is leaving so you can instantly get to the root cause of any issue with OpenReplay — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data.
Star on GitHub12k