Created
February 22, 2020 23:08
-
-
Save cenkce/90cdab98d5499c77c4aaeee882cabb74 to your computer and use it in GitHub Desktop.
Typescript Tuple Operators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type SliceTuple< | |
T extends any[], | |
TMaxItem extends number = 2, | |
R extends any[] = [], | |
P extends any[] = [], | |
I extends any[] = [] | |
> = { | |
0: SliceTuple<T, TMaxItem, R, Prepend<T[Pos<I>], P>, Next<I>>; | |
1: Reverse<Prepend<Reverse<P>, R>>; | |
2: SliceTuple<T, TMaxItem, Prepend<Reverse<P>, R>, [T[Pos<I>]], Next<I>>; | |
// if empty value | |
3: []; | |
}[T[0] extends undefined | |
? 3 | |
: Pos<I> extends Length<T> | |
? 1 | |
: Length<P> extends TMaxItem | |
? 2 | |
: 0]; | |
type MergeTuple< | |
T extends any[], | |
R extends { [key: string]: any } = {}, | |
I extends any[] = [] | |
> = { | |
0: MergeTuple<T, { [key in T[Pos<I>][0]]: T[Pos<I>][1] } & R, Next<I>>; | |
1: R; | |
}[Pos<I> extends [] ? 1 : Pos<I> extends Length<T> ? 1 : 0]; | |
type PipeTuple<T extends any[], R extends any[] = [], I extends any[] = []> = { | |
0: PipeTuple<T, T[Pos<I>] | R, Next<I>>; | |
1: R; | |
}[T[0] extends undefined | |
? 1 | |
: Pos<I> extends [] | |
? 1 | |
: Pos<I> extends Length<T> | |
? 1 | |
: 0]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment