Curry Types Preserve Parameter Types
Model curried functions with recursive conditional types so each partial application peels one parameter and keeps the remaining types.
Code
// Curry types: each call peels one parameter type
type Curry<F> = F extends (...args: infer A) => infer R
? A extends [infer Head, ...infer Tail]
? Tail extends []
? (arg: Head) => R
: (arg: Head) => Curry<(...args: Tail) => R>
: () => R
: never;
function curry<F extends (...args: never[]) => unknown>(fn: F): Curry<F> {
function collect(collected: unknown[]): (...next: unknown[]) => unknown {
return (...next: unknown[]) => {
const merged = [...collected, ...next];
if (merged.length >= fn.length) {
return (fn as unknown as (...args: unknown[]) => unknown)(...merged);
}
return collect(merged);
};
}
return collect([]) as Curry<F>;
}
function join3(a: string, b: string, c: string): string {
return a + '-' + b + '-' + c;
}
const curried = curry(join3);
const step1 = curried('2026');
const step2 = step1('11');
console.log(step2('17'));
// 偏应用:分两次给齐参数
const partial = curried('a')('b');
console.log(partial('c'));
// 参数类型逐级保留,错类型编译失败
// curried(1);Output
Captured from a sandboxed run of this snippet in the build pipeline.
2026-11-17
a-b-cThe parts that do the work
Curry Types Preserve Parameter Types keeps everything in the functions collect and join3.
What to watch for
- The guard
merged.length >= fn.lengthshort-circuits the flow with an earlyreturnwhen its condition holds.
Applying it
The primary signature is function collect(collected: unknown[])
Statements that matter
return (fn as unknown as (...args: unknown[]) => unknown)(...merged);
type Curry<F> = F extends (...args: infer A) => infer R
? A extends [infer Head, ...infer Tail]
? Tail extends []
? (arg: Head) => R
: (arg: Head) => Curry<(...args: Tail) => R>
: () => R
: never;
return collect([]) as Curry<F>;
Interactive Tools for This Topic
FAQ
- Anything to download besides the language toolchain?
- Nothing beyond the toolchain you already have for this language. The file defines `collect`, `join3` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.