Ever need a Type for a curried style function? Here's how you do it!
// Type helper to define curried function
type Func<T extends readonly any[]> = T["length"] extends 0
? () => void
: T["length"] extends 1
? () => T[0]
: (
x: T[0]
) => T extends readonly [infer _, ...infer V]
? V["length"] extends 1
? V[0]
: Func<V>
: never;
// Usage
const func0: Func<[]> = () => undefined;
const func1: Func<[string]> = () => "";
const func2: Func<[string, number]> = (str) => 0;
const func3: Func<[number, boolean, string]> = (num) => (bool) => "";
Top comments (0)