Skip to content

Instantly share code, notes, and snippets.

@freddi301
Created June 8, 2019 01:25
Show Gist options
  • Select an option

  • Save freddi301/4439b221e586f9740f98d8bdf1c0c952 to your computer and use it in GitHub Desktop.

Select an option

Save freddi301/4439b221e586f9740f98d8bdf1c0c952 to your computer and use it in GitHub Desktop.
Typescript named curry functino
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