diff --git a/intersection.ts b/intersection.ts new file mode 100644 index 0000000..6a51ebd --- /dev/null +++ b/intersection.ts @@ -0,0 +1,31 @@ +import type { Equal, Expect } from '@type-challenges/utils' + +type cases = [ + Expect, 2>>, + Expect, 2 | 3>>, + Expect, never>>, + Expect, 3>>, + Expect, 2 | 3>>, + Expect, never>>, +]; + +type ToUnion = T[number]; + +type Intersect = T & U; + +type Intersection< + T +> = +T extends [infer First, infer Second, ...infer Tail] + ? First extends number[] + ? Second extends number[] + ? Intersection<[Intersect, ToUnion>, ...Tail]> + : Intersection<[Intersect, Second>, ...Tail]> + : Intersection<[Intersect, ...Tail]> + : T extends [infer Head, ...infer Tail] + ? Head extends number[] + ? ToUnion + : Head + : never +; +