Last active
February 22, 2020 23:07
-
-
Save cenkce/e85e25aa979f9d6995247fa02844981e to your computer and use it in GitHub Desktop.
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
// ------- Typing Operators ------- | |
// https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts | |
export type Iterator< | |
Index extends number = 0, | |
From extends any[] = [], | |
I extends any[] = [] | |
> = { | |
0: Iterator<Index, Next<From>, Next<I>>; | |
1: From; | |
}[Pos<I> extends Index ? 1 : 0]; | |
export type Length<T extends any[]> = T["length"]; | |
export type Prepend<E, T extends any[]> = (( | |
head: E, | |
...args: T | |
) => any) extends (...args: infer U) => any | |
? U | |
: T; | |
export type Pos<I extends any[]> = Length<I>; | |
export type Reverse< | |
T extends any[], | |
R extends any[] = [], | |
I extends any[] = [] | |
> = { | |
0: Reverse<T, Prepend<T[Pos<I>], R>, Next<I>>; | |
1: R; | |
}[Pos<I> extends Length<T> ? 1 : 0]; | |
export type Concat<T1 extends any[], T2 extends any[]> = Reverse< | |
Reverse<T1> extends infer R ? Cast<R, any[]> : never, | |
T2 | |
>; | |
export type Next<I extends any[]> = Prepend<any, I>; | |
export type Cast<X, Y> = X extends Y ? X : Y; | |
export type Append<E, T extends any[]> = Concat<T, [E]>; | |
// End of Typing Operators------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment