Created
April 1, 2023 18:57
-
-
Save chenasraf/f69a9a4e0a66dce13f261794b7d912c5 to your computer and use it in GitHub Desktop.
TS compose fn
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 FirstOf<T extends IOFn[]> = T extends [infer F extends (...args: any) => any, ...IOFn[]] | |
? Parameters<F> | |
: never | |
type LastOf<T extends IOFn[]> = T extends [...IOFn[], infer L extends (...args: any) => any] | |
? ReturnType<L> | |
: never | |
type IOFn<I extends any[] = any, O = any> = (...x: I) => O | |
type Composed<A extends IOFn[]> = (...x: FirstOf<A>) => LastOf<A> | |
export function compose<A extends IOFn[]>(...fns: A): Composed<A> { | |
return (...x: any[]) => fns.reduceRight((acc, fn) => [fn(...acc)], x)[0] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment