You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1000 B

import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Intersection<[[1, 2], [2, 3], [2, 2]]>, 2>>,
Expect<Equal<Intersection<[[1, 2, 3], [2, 3, 4], [2, 2, 3]]>, 2 | 3>>,
Expect<Equal<Intersection<[[1, 2], [3, 4], [5, 6]]>, never>>,
Expect<Equal<Intersection<[[1, 2, 3], [2, 3, 4], 3]>, 3>>,
Expect<Equal<Intersection<[[1, 2, 3], 2 | 3 | 4, 2 | 3]>, 2 | 3>>,
Expect<Equal<Intersection<[[1, 2, 3], 2, 3]>, never>>,
];
type ToUnion<T extends unknown[]> = T[number];
type Intersect<T, U> = T & U;
type Intersection<
T
> =
T extends [infer First, infer Second, ...infer Tail]
? First extends number[]
? Second extends number[]
? Intersection<[Intersect<ToUnion<First>, ToUnion<Second>>, ...Tail]>
: Intersection<[Intersect<ToUnion<First>, Second>, ...Tail]>
: Intersection<[Intersect<First, Second>, ...Tail]>
: T extends [infer Head, ...infer Tail]
? Head extends number[]
? ToUnion<Head>
: Head
: never
;