From 1136e6c59eb43ae1c0e8c26a80573155a5e00842 Mon Sep 17 00:00:00 2001 From: MrSpoony Date: Thu, 9 Mar 2023 09:27:06 +0100 Subject: [PATCH] add intersection --- intersection.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 intersection.ts 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 +; +