TypeScript Academy · Lesson

Tuple rest elements, curry types

Use tuple rest elements to capture arbitrary argument lists and build small curry helpers with type inference.

Lesson 1 of 38 steps

Tuple rest elements, curry types is a free TypeScript Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.

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 → string

Curry helper

A little curry: Thanks to [...A, L], both the head and last types are correct.

function curryLast<A extends any[], L, R>(fn: (...args: [...A, L]) => R) {
  return (last: L) => (...head: A) => fn(...head, last)
}

const curried = curryLast(join3)
const out = curried(true)("x", 42) // string

General curryFromFn

General solution: Infer Tail/Last from Parameters<F>, thus preserving the number of parameters and their types.

type Fn = (...args: any[]) => any

type Tail<T extends any[]> = T extends [...infer H, any] ? H : never

type Last<T extends any[]> = T extends [...any[], infer L] ? L : never

function curryFromFn<F extends Fn>(fn: F) {
  type A = Tail<Parameters<F>>
  type L = Last<Parameters<F>>
  return (last: L) => (...head: A) => fn(...head, last)
}

const cur = curryFromFn(join3)
const o2 = cur(true)("x", 42) // still string

Compose with tuples

Flow A → B → C with compose: A tuple arguments are preserved, output types are chained.

function compose<A extends any[], B, C>(g: (b: B) => C, f: (...args: A) => B) {
  return (...args: A): C => g(f(...args))
}

function len(a: string, b: number) { return `${a}${b}`.length }
function isEven(n: number) { return n % 2 === 0 }

const comp = compose(isEven, len)
const ok = comp("id", 99) // boolean

Tips

Tips:

Variadic tuples require TS 4.0+; keep the target up to date.

Hint the compiler: sometimes improve inference by explicitly writing type parameters.

Over-curiousness adds complexity to API design; simple helpers are most effective.

Curry signature check

Quick check: Which signature captures any args A and the last arg L for a curry helper?

Recap

Recap: Model argument lists with [...A, L]; write small curry and compose helpers; types are automatically preserved.

Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “Tuple rest elements, curry types” lesson free?

Yes — the full text of “Tuple rest elements, curry types” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 3 lessons in total.

What will I learn in “Tuple rest elements, curry types”?

Use tuple rest elements to capture arbitrary argument lists and build small curry helpers with type inference. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “Tuple rest elements, curry types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Tuple rest elements, curry types
  2. Compose functions and arguments safely
  3. Strongly-typed builders
← Back to TypeScript Academy