Sub topic 10
Labeled Tuple Elements
Prerequisites and purpose
This chapter assumes you know basic TypeScript annotations, union types, arrays, and tuple types. A tuple is an array whose length and position types are known.
A labeled tuple adds names to tuple positions:
type SearchOptions = [query: string, limit?: number];Here, query labels the first position and limit labels the optional second position. The labels describe the tuple's meaning. They do not create object-like properties, and they do not change the tuple's underlying type.
Making tuple positions readable
Without labels, a function parameter tuple can be difficult to understand:
type Unclear = [string, number?];The reader must guess what the string and number represent. Labels make the intended use visible:
type SearchOptions = [query: string, limit?: number];
function search(...options: SearchOptions): string[] {
const [query, limit = 10] = options;
return [`Searching for "${query}" with limit ${limit}`];
}
console.log(search("typescript"));
console.log(search("tuples", 5));The labels also appear in editor hints when you construct or pass the tuple. This makes labeled tuples useful for public function APIs and reusable type aliases.
Labels must follow the tuple's existing structure:
| Tuple feature | Labeled syntax | Meaning |
|---|---|---|
| Required element | `[id: number]` | A required number at this position |
| Optional element | `[id?: number]` | A number or omitted position |
| Rest element | `[first: string, ...values: number[]]` | One string followed by any number of numbers |
A rest element must be last. Optional elements must come before a rest element.
Labels do not affect compatibility
Tuple labels are documentation for developers and tools. TypeScript does not use them when checking whether two tuple types are compatible.
type Coordinates = [x: number, y: number];
type UnlabeledCoordinates = [number, number];
const point: Coordinates = [10, 20];
const sameShape: UnlabeledCoordinates = point;Coordinates and UnlabeledCoordinates have the same element types and length. The names x and y do not become accessible properties:
const [x, y] = point;
console.log(x, y);
// point.x would be an errorUse an object when callers need named property access, especially when the values have many fields or may change order:
type PointObject = {
x: number;
y: number;
};Use a labeled tuple when position is meaningful and the value is short, such as coordinates, function arguments, or a pair of related results.
Labels in function parameter lists
A tuple can describe a function's parameter list. Labels then document each argument in the function signature:
function connect(host: string, port: number): string {
return `${host}:${port}`;
}
type ConnectParameters = [host: string, port: number];
const parameters: ConnectParameters = ["localhost", 8080];
console.log(connect(...parameters));The built-in Parameters utility type can also preserve parameter labels from an existing function:
type Arguments = Parameters<typeof connect>;
// [host: string, port: number]Labels are especially helpful when a function uses optional or rest parameters because they explain what each position means.
Exercise
Do labels change the type compatibility of [x: number, y: number] compared with [number, number]?
Show answer
No. Labels are metadata for readability and editor hints. Both tuples have the same length and element types, so they are compatible.