Skip to content

Instantly share code, notes, and snippets.

@Schniz
Created June 25, 2019 10:06
Show Gist options
  • Save Schniz/06888eb3313fc3379f7c18d8a09c13fb to your computer and use it in GitHub Desktop.
Save Schniz/06888eb3313fc3379f7c18d8a09c13fb to your computer and use it in GitHub Desktop.
function add2(a: number, b: number) {
return a + b;
}
function add4(a: number, b: number, c: number, d: number) {
return a + b + c + d;
}
function concat(a: string, b: string) {
return (a + b).length;
}
function slice<T>(arr: T[], from: number) {
return arr.slice(from);
}
type AddOne<T extends number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13][T];
type Arity<F extends (...args: any) => any> = Parameters<F> extends {
length: infer R;
}
? R
: never;
type CurriedFunction<
F extends (...args: any) => any,
From extends number = 0
> = Parameters<F>[From] extends undefined
? ReturnType<F>
: (x: Parameters<F>[From]) => CurriedFunction<F, AddOne<From>>;
type CurriedAdd2 = CurriedFunction<typeof add2>;
type CurriedAdd4 = CurriedFunction<typeof add4>;
type CurriedConcat = CurriedFunction<typeof concat>;
type CurriedSlice = CurriedFunction<typeof slice>; // unknown[] :((((((
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment