0Pricing
TypeScript Academy · Lesson

Typing Variadic Functions

Build flexible APIs with variadic tuple types.

Typing Variadic Functions is a free TypeScript Academy lesson on CoddyKit — lesson 4 of 4. 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Generic Variadic Functions

Variadic tuple types let generics capture and manipulate argument lists. This unlocks utilities that concatenate tuples, forward parameters, and reshape call signatures with full type safety.

Capturing Arguments as a Tuple

A generic constrained to unknown[] can capture an entire argument list as a tuple type T, preserving each parameter's type and position.

function tuple<T extends unknown[]>(...args: T): T {
  return args
}
const t = tuple("a", 1, true) // type [string, number, boolean]
console.log(t)

Spreading a Generic Tuple

You can spread a captured generic tuple into another parameter list. This forwards arguments while keeping their exact types.

function wrap<T extends unknown[]>(fn: (...args: T) => void) {
  return (...args: T) => fn(...args)
}
const log = wrap((n: number, s: string) => console.log(n, s))
log(1, "hi")

Concatenating Two Tuples

Variadic tuple types shine when combining tuples. Spreading two generic tuples in a return type produces their concatenation.

function concat<A extends unknown[], B extends unknown[]>(a: A, b: B): [...A, ...B] {
  return [...a, ...b]
}
const r = concat([1, 2], ["a"]) // [number, number, string]
console.log(r)

Why the Result Type Is Precise

Because [...A, ...B] spreads the generic tuples, the result preserves each element's type and order — not just a widened union array.

function concat<A extends unknown[], B extends unknown[]>(a: A, b: B): [...A, ...B] {
  return [...a, ...b]
}
const r = concat([true], [42])
const flag: boolean = r[0]
const num: number = r[1]
console.log(flag, num)

Prepending an Argument

Variadic tuples let you reshape parameter lists — for example, a function that adds a leading argument to a callback's signature.

function withId<T extends unknown[]>(fn: (...args: T) => void) {
  return (id: number, ...rest: T) => {
    console.log("id", id)
    fn(...rest)
  }
}
const f = withId((msg: string) => console.log(msg))
f(7, "hello")

Extracting the Head Type

With a tuple parameter, you can split off the first element type and the rest using a labeled tuple, then operate on each part.

function head<H, T extends unknown[]>(...args: [H, ...T]): H {
  return args[0]
}
const h = head("first", 2, 3) // type string
console.log(h)

Parameters Utility Type

The built-in Parameters<F> extracts a function's argument list as a tuple — itself built on variadic tuple types. You can reuse a signature's parameters elsewhere.

function greet(name: string, age: number) {}
type GreetArgs = Parameters<typeof greet> // [string, number]
const args: GreetArgs = ["Ada", 36]
console.log(args)

Forwarding With Parameters

Combine Parameters with a rest spread to build a wrapper that forwards arguments to an existing function with identical typing.

function add(a: number, b: number) { return a + b }
function traced(...args: Parameters<typeof add>): number {
  console.log("calling add", args)
  return add(...args)
}
console.log(traced(2, 3))

Building a Curry-Like Signature

Variadic tuples enable partial-application helpers: capture some arguments now, expect the rest later, all type-checked.

function partial<A extends unknown[], B extends unknown[], R>(
  fn: (...args: [...A, ...B]) => R,
  ...a: A
) {
  return (...b: B) => fn(...a, ...b)
}
const addThree = (x: number, y: number, z: number) => x + y + z
const add5 = partial(addThree, 5)
console.log(add5(2, 3)) // 10

Power and Caution

Variadic tuple generics give you precise, reusable function-shaping tools — wrappers, forwarders, concatenators. They are advanced, so reach for them when the type precision genuinely pays off.

function pipe2<A extends unknown[], B, C>(
  f: (...a: A) => B,
  g: (b: B) => C
) {
  return (...a: A) => g(f(...a))
}
const fn = pipe2((n: number) => n * 2, (n) => n + 1)
console.log(fn(5)) // 11

Quick Check

Test your understanding of variadic tuple generics.

Recap

Variadic tuple types let generics capture argument lists as tuples and spread them in parameter or return positions. This powers tuple concatenation ([...A, ...B]), argument forwarding, head/tail splitting, and partial application — all with exact element types preserved. Built-ins like Parameters<F> rely on the same machinery. Use these tools when precise function-shape typing is worth the complexity.

Frequently asked questions

Is the “Typing Variadic Functions” lesson free?

Yes — the full text of “Typing Variadic Functions” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. 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.

What will I learn in “Typing Variadic Functions”?

Build flexible APIs with variadic tuple types. 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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Typing Variadic Functions” 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. Rest Parameters in Functions
  2. Spread in Arrays and Objects
  3. Tuple Rest Elements
  4. Typing Variadic Functions
← Back to TypeScript Academy