Skip to content
Onsite

UnionToIntersection<U>

HardP2Coding

Implement UnionToIntersection<U>: convert a union type into the intersection of its members. For example { a: 1 } | { b: 2 } becomes { a: 1 } & { b: 2 }.

Tests your type must pass

type cases = [
  Expect<Equal<UnionToIntersection<{ a: 1 } | { b: 2 }>, { a: 1 } & { b: 2 }>>,
  Expect<Equal<UnionToIntersection<(() => 'foo') | ((i: 42) => true)>, (() => 'foo') & ((i: 42) => true)>>,
  Expect<Equal<UnionToIntersection<{ x: string } | { y: number } | { z: boolean }>, { x: string } & { y: number } & { z: boolean }>>,
  Expect<Equal<UnionToIntersection<string>, string>>,
  Expect<Equal<UnionToIntersection<'foo' | 42 | true>, never>>,
]

Hints

TypeScript
Run and Submit type-check your solution against the tests.