Tuple rest elements, curry types
Use tuple rest elements to capture arbitrary argument lists and build small curry helpers with type inference.
Intro
Goal: Capture argument lists with variadic tuples and write a tiny but useful curry helper. Bu sayede API'leriniz esnek kalır, tip güvenliği korunur.
Variadic basics
The [...A, L] pattern: first the head arguments (A tuple), then the last argument (L). It is the cornerstone for helpers.
type HeadTail<A extends any[], L> = [...A, L]
function callWithLast<A extends any[], L, R>(fn: (...args: [...A, L]) => R, last: L, ...head: A): R {
return fn(...head, last)
}
// Example
function join3(a: string, b: number, c: boolean) { return `${a}-${b}-${c}` }
const r = callWithLast(join3, true, "x", 42) // ok → stringAll lessons in this course
- Tuple rest elements, curry types
- Compose functions and arguments safely
- Strongly-typed builders