QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Union to Intersection

Converts a union type into an intersection type.

  • helper-types
  • typedefinition
  • intersection
  • union

Install

npx shadcn@latest add https://quicksnip.dev/r/union-to-intersection.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

type UnionToIntersection<U> = (U extends any ? (arg: U) => void : never) extends (arg: infer I) => void
    ? I
    : never;

Usage

type A = { id: string };
type B = { name: string };
type C = { age: number };

type Intersected = UnionToIntersection<A | B | C>;
// { id: string } & { name: string } & { age: number }