Codectionary / Developer documentation / TypeScript

Variadic Tuple Types

Variadic tuples model tuple composition with generic rest elements. Added in TypeScript 4.0, they let helpers preserve the types and positions of an arbitrary argument tail. They avoid long lists of overloads for every possible tuple length.

Syntax

function tail<T extends unknown[]>(items: [unknown, ...T]): T { /* ... */ }

Examples

Variadic Tuple Types: working example

Run this standalone example after compiling with TypeScript 5.4+ in strict mode, targeting ES2022. Expected result: The program prints 7 true; result retains the tuple type [number, boolean].

function tail<T extends unknown[]>(items: [unknown, ...T]): T {
  const [, ...rest] = items;
  return rest;
}
const input: [string, number, boolean] = ["header", 7, true];
const result = tail(input);
const count: number = result[0];
const enabled: boolean = result[1];
console.log(count, enabled);
  1. Expected result: The program prints 7 true; result retains the tuple type [number, boolean].
  2. The first unknown slot is removed while T captures all remaining positions.
  3. Unlike a broad array union, the resulting tuple remembers which position is numeric.

Best practices

  • Use variadic tuples when positional relationships matter.
  • Prefer named object parameters for APIs with many semantically unrelated arguments.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading