Skip to content

Instantly share code, notes, and snippets.

@emiaj
Created August 17, 2018 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emiaj/e8eeb3546de8790c76bf829669bc281d to your computer and use it in GitHub Desktop.
Save emiaj/e8eeb3546de8790c76bf829669bc281d to your computer and use it in GitHub Desktop.
Curry
declare type Curry<P, V> =
P extends [infer A, infer B, infer C, infer D] ? (a: A) => Curry<[B, C, D], V> :
P extends [infer A, infer B, infer C] ? (a: A) => Curry<[B, C], V> :
P extends [infer A, infer B] ? (a: A) => Curry<[B], V> :
P extends [infer A] ? (a: A) => V :
() => V; // could be unknow, but this allows to safely pass parameterless functions
declare function curry<T extends any[], R>(fn: (...args: T) => R): Curry<T, R>;
const add = curry((a: number, b: number, c: number) => a + b + c);
add(1)(2)(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment