TypeScript Reference ยท Literal and Object Types
TypeScript labeled and variadic tuples
Reference for TypeScript labeled tuple elements, optional and rest elements, and variadic tuple types that spread other tuples to build typed concat and head helpers.
A tuple is an array type with a fixed layout: each position has its own type. Labeled tuple elements give each position a name ([x: number, y: number]). The labels are documentation only: they show up in editor hints and error messages and in function signatures built from tuples, but they do not change assignability.
Variadic tuple types spread another tuple or array type inside a tuple ([...A, ...B]), which lets you describe operations such as concatenation, prepending and splitting entirely at the type level.
type Point = [x: number, y: number, z?: number]; type Concat<A extends unknown[], B extends unknown[]> = [...A, ...B];
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type L = [label: Type, label2?: Type, ...rest: Type[]]; type V = [...T, ...U]; // T and U are tuple or array types type M = [first: string, ...middle: number[], last: boolean];
| Feature | Description |
|---|---|
| Labels | Either all elements are labeled or none are; mixing is an error. |
Optional ? |
Must follow required elements; the tuple's length is then a union of possible lengths. |
Rest ... |
At most one rest element of array type, anywhere (leading, middle or trailing). |
| Spread of a generic | [...T] where T extends unknown[] keeps the tuple shape when inferred. |
Edge cases: spreading a non-tuple array type produces a rest element ([...string[], number]). Only one array-type spread may appear; a second one at the same level is an error. Labels may be used on spread elements as ...name: Type[].
Usage notes
- Labels do not matter for compatibility:
[a: number]and[b: number]are the same type. - Variadic inference: annotate parameters as
[...A](not justA) so TypeScript infers a tuple rather than an array. - Function parameters:
Parameters<F>returns a labeled tuple, so hover hints keep parameter names. - Readonly:
readonly [number, string]forbids mutation;as constproduces readonly tuples with literal element types. noUncheckedIndexedAccessaffects arrays and rest elements, not fixed tuple positions.- Recursion limit: recursive tuple types (for example building a length-N tuple) hit instantiation depth limits at around a thousand levels.