typescript / expert
Snippet
Type-Level Currying with Variadic Tuple Inference
Variadic tuples let you treat a function's parameter list as a generic tuple and peel parameters off one at a time. The Curry mapped type recurses through the tuple: each step returns a unary function whose return type is either the final result R or the next curried step. Because the parameter tuple is captured generically, names and types of every argument propagate end-to-end without any loss of inference, giving you a fully type-safe curry helper that works for any arity.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
type Curry<P extends unknown[], R> =P extends [infer H, ...infer T]? T extends [] ? (arg: H) => R : (arg: H) => Curry<T, R>: R;declare function curry<P extends unknown[], R>(fn: (...args: P) => R): Curry<P, R>;const add = (a: number, b: number, c: number) => a + b + c;const curried = curry(add);const result = curried(1)(2)(3);
Breakdown
1
type Curry<P extends unknown[], R> =
Mapped type that converts a tuple of parameters P and return type R into a curried signature.
2
P extends [infer H, ...infer T]
Pattern-matches the head H and rest T off the parameter tuple using variadic inference.
3
? T extends [] ? (arg: H) => R : (arg: H) => Curry<T, R>
Base case returns the final R; otherwise recurse with the remaining parameters.
4
const result = curried(1)(2)(3);
Each call narrows the tuple; the final application is inferred as number.