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);- Expected result: The program prints 7 true; result retains the tuple type [number, boolean].
- The first unknown slot is removed while T captures all remaining positions.
- 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