Last active
November 9, 2021 16:13
-
-
Save k-tten/c4a3ec041a09257b1fa10df7c748aafe to your computer and use it in GitHub Desktop.
Arbitrary arity pipe function typings
This file contains 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 UnaryFunction<T, R> = (source: T) => R; | |
type Pipe<T, L = void, R extends UnaryFunction<any, any>[] = []> = | |
T extends [] | |
? R | |
: L extends void | |
? T extends [(_: infer P) => infer V, ...infer Rest] | |
? Pipe<Rest, V, [...R, UnaryFunction<P, V>]> | |
: never[] | |
: T extends [(_: L) => infer V, ...infer Rest] | |
? Pipe<Rest, V, [...R, UnaryFunction<L, V>]> | |
: never[] | |
; | |
type Compose<A> = A extends [UnaryFunction<infer S, any>, ...UnaryFunction<any, any>[], UnaryFunction<any, infer V>] ? UnaryFunction<S, V> : never; | |
type Valid<A> = A extends UnaryFunction<any, any>[] ? Pipe<A> extends never[] ? never[] : A : never[]; | |
declare function pipe<A extends UnaryFunction<any, any>[]>(...fns: Valid<A>): Compose<Pipe<A>>; | |
pipe( | |
(_: string) => 0, | |
(_: number) => "", | |
(_: string) => true, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
note: haven't gotten to validation of arguments yetnow added :)