Created
June 8, 2019 01:25
-
-
Save freddi301/4439b221e586f9740f98d8bdf1c0c952 to your computer and use it in GitHub Desktop.
Typescript named curry functino
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 Tequal<A, B> = A extends B ? (B extends A ? true : false) : false; | |
| type Bind<P extends Record<string, any>, R> = Tequal<P, {}> extends false | |
| ? (<K extends keyof P>( | |
| key: K, | |
| value: P[K] | |
| ) => Bind<Pick<P, Exclude<keyof P, K>>, R>) | |
| : () => R; | |
| function curryNamed<Params extends Record<string, any>, R>( | |
| fun: (p: Params) => R | |
| ): Bind<Params, R> { | |
| const params: Partial<Params> = {}; | |
| const bind = (key: any, value: any) => { | |
| if (!key) { | |
| return fun(params as Required<Params>); | |
| } | |
| params[key] = value; | |
| return bind; | |
| }; | |
| return bind as any; | |
| } | |
| const a = curryNamed((p: { a: number; b: Date; c: string }) => { | |
| return "it works" as const; | |
| }); | |
| const b = a("a", 45)("c", "hello")("b", new Date())(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment