Skip to content

Commit

Permalink
feat: improve IntersectTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosenstein committed Apr 15, 2019
1 parent a756c7b commit 2e3ae20
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/types/tuples.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {UnionToIntersection} from './utils';

export interface Vector<T> { readonly [x: number]: T; readonly length: number; }
export type Length<T extends Vector<any>> = T['length'];

Expand All @@ -12,15 +14,4 @@ export type UnionizeTuple<T extends Vector<any>> = T[number];
* @param T a tuple of items up to 10
* @returns an intersection of all items in the tuple
*/
export type IntersectTuple<T extends Vector<any>> =
Length<T> extends 1 ? T[0] :
Length<T> extends 2 ? T[0] & T[1] :
Length<T> extends 3 ? T[0] & T[1] & T[2] :
Length<T> extends 4 ? T[0] & T[1] & T[2] & T[3] :
Length<T> extends 5 ? T[0] & T[1] & T[2] & T[3] & T[4] :
Length<T> extends 6 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] :
Length<T> extends 7 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] :
Length<T> extends 8 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] :
Length<T> extends 9 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] :
Length<T> extends 10 ? T[0] & T[1] & T[2] & T[3] & T[4] & T[5] & T[6] & T[7] & T[8] & T[9] :
any;
export type IntersectTuple<T extends Vector<any>>= UnionToIntersection<T[number]>;

0 comments on commit 2e3ae20

Please sign in to comment.