typescript / expert
Snippet
Recursive Tuple Inference for Auto-Curried Function Signatures
Recursive conditional type mapping using infer and variadic tuple spreads transforms multi-argument function parameter lists into curried unary functions while preserving precise parameter types at each curried step.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
type Curry<Args extends any[], Return> = Args extends [infer Head, ...infer Tail]? (arg: Head) => Tail extends [] ? Return : Curry<Tail, Return>: () => Return;function curryTwo<A, B, R>(fn: (a: A, b: B) => R): Curry<[A, B], R> {return (a: A) => (b: B) => fn(a, b);}const add = (x: number, y: number): number => x + y;const curriedAdd = curryTwo(add);const addFive = curriedAdd(5);const result = addFive(10);
Breakdown
1
type Curry<Args extends any[], Return> = Args extends [infer Head, ...infer Tail]
Uses template tuple matching to extract the first argument type into Head and remaining into Tail.
2
? (arg: Head) => Tail extends [] ? Return : Curry<Tail, Return>
Recursively resolves single-argument function return types until Tail is empty.
3
function curryTwo<A, B, R>(fn: (a: A, b: B) => R): Curry<[A, B], R> {
Applies the Curry utility type to a binary function signature.
4
return (a: A) => (b: B) => fn(a, b);
Returns nested unary arrow functions matching the typed curry structure.