Sub topic 6
Multiple Candidates: Unions, Intersections and UnionToIntersection
This chapter assumes you know TypeScript generics, unions, intersections, function signatures, and conditional types. It focuses on what happens when one infer variable receives more than one possible type.
Covariant positions produce unions
A type position is covariant when values flow out of it. Return types and object properties are common covariant positions. If TypeScript finds several candidates for the same inferred variable in these positions, it usually combines them into a union.
type PropertyCandidates<T> =
T extends {
first: infer U;
second: infer U;
}
? U
: never;
type Result = PropertyCandidates<{
first: string;
second: number;
}>;
// string | numberBoth properties provide a candidate for U. Since either property can contain its own type, TypeScript preserves both possibilities as string | number.
The same idea appears when extracting return types from a union of functions:
type ReturnOf<T> =
T extends (...args: any[]) => infer R
? R
: never;
type Result = ReturnOf<
(() => string) | (() => number)
>;
// string | numberThe conditional type distributes over the function union. It extracts string from the first function and number from the second, then combines the results.
Contravariant positions produce intersections
A function parameter is a contravariant position. It describes what a function must be able to accept. When TypeScript infers the same variable from multiple parameter candidates, it combines those candidates as an intersection.
An intersection means that a value must satisfy all included types:
type A = { a: 1 };
type B = { b: 2 };
type Both = A & B;
// { a: 1 } & { b: 2 }
const value: Both = {
a: 1,
b: 2,
};This behavior lets us transform a union into an intersection.
Building UnionToIntersection
The standard technique wraps every union member in a function parameter:
type UnionToIntersection<U> =
(U extends unknown
? (arg: U) => void
: never) extends (arg: infer I) => void
? I
: never;
type Parts = { a: 1 } | { b: 2 };
type Merged = UnionToIntersection<Parts>;
// { a: 1 } & { b: 2 }
const merged: Merged = {
a: 1,
b: 2,
};The inner conditional is distributive because U appears directly on the left side of extends. For Parts, it produces:
(arg: { a: 1 }) => void
| (arg: { b: 2 }) => voidThe outer conditional compares that function union with one function type containing infer I. The inferred variable appears in a parameter position, so the candidates combine contravariantly. The result is { a: 1 } & { b: 2 }.
Common errors
Do not confuse an intersection with a union. A union accepts either shape, while an intersection requires every shape.
type Either = { a: 1 } | { b: 2 };
type Every = { a: 1 } & { b: 2 };
const eitherA: Either = { a: 1 };
const eitherB: Either = { b: 2 };
const every: Every = { a: 1, b: 2 };Also, UnionToIntersection is most useful for object-like unions. Applying it to incompatible primitive types can produce an unusable result:
type Impossible = UnionToIntersection<string | number>;
// string & number, effectively neverCompile-time checks
You can verify the result without creating runtime code:
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2)
? true
: false;
type Expect<T extends true> = T;
type _Check = Expect<
Equal<
UnionToIntersection<{ a: 1 } | { b: 2 }>,
{ a: 1 } & { b: 2 }
>
>;Exercise
Why does UnionToIntersection place the inferred type in a function parameter?
Show answer
Function parameters are contravariant, so multiple inferred candidates combine into an intersection instead of a union.