Skip to content

Instantly share code, notes, and snippets.

@moimikey
Last active August 14, 2025 20:47
Show Gist options
  • Save moimikey/700e61bd52f3f4ff684e568504a9c6f3 to your computer and use it in GitHub Desktop.
Save moimikey/700e61bd52f3f4ff684e568504a9c6f3 to your computer and use it in GitHub Desktop.
export type Curry<F extends (...args: readonly unknown[]) => unknown> =
Parameters<F> extends readonly [infer First, ...infer Rest]
? (arg: First) => Rest extends readonly []
? ReturnType<F>
: Curry<(...args: Rest) => ReturnType<F>>
: ReturnType<F>;
export function curry<F extends (...args: readonly unknown[]) => unknown>(fn: F): Curry<F> {
function curried(...providedArgs: readonly unknown[]): unknown {
if (providedArgs.length >= fn.length) {
return fn(...(providedArgs.slice(0, fn.length) as Parameters<F>));
}
return (...newArgs: readonly unknown[]) => {
return curried(...providedArgs, ...newArgs);
};
}
return curried as Curry<F>;
}
@moimikey
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment