Skip to content

UnionToIntersection

Converts a union type to an intersection type: A | B | C → A & B & C.

Uses conditional-type distribution and the contravariant position of a function parameter to collapse the union into an intersection.

Available since v3.0.0

import type { UnionToIntersection } from '@helpers4/type';
// or, from the all-in-one package (same code, one install):
import type { UnionToIntersection } from 'helpers4/type';
type UnionToIntersection<U> =
  (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never
type Union = { a: number } | { b: string } | { c: boolean };
type Intersection = UnionToIntersection<Union>;
// { a: number } & { b: string } & { c: boolean }

type Keys = UnionToIntersection<'a' | 'b'>;
// 'a' & 'b'  (resolves to never for disjoint literals)

View source on GitHub