parent
89a725f664
commit
1136e6c59e
@ -0,0 +1,31 @@
|
||||
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
|
||||
;
|
||||
|
||||
Loading…
Reference in new issue