Named Tuple Labels Syntax
What you'll learn: How to add descriptive labels to tuple element positions using the label: type notation for clearer, more self-documenting tuple types.
The Problem: Position-Based Confusion
You already know how to create tuple types with specific positions:
type Coordinate = [number, number];
But which number is x and which is y? The type system knows they're numbers at position 0 and 1, but you have to remember what each position means. When tuples grow beyond 2-3 elements, this becomes error-prone.
The Solution: Named Labels
TypeScript lets you attach descriptive labels to each tuple position. The syntax is simple:
type Coordinate = [x: number, y: number];
The labels x: and y: don't change the runtime behavior at all—they're purely for documentation and tooling. Think of them like parameter names in a function signature: they guide you without enforcing anything at runtime.
How It Works
type Response = [status: number, body: string, headers: Record<string, string>];
function parseResponse(): Response {
return [200, "Success", { "Content-Type": "application/json" }];
}
const result: Response = parseResponse();
// Hover over 'result' and your IDE shows:
// [status: number, body: string, headers: Record<string, string>]
The labels appear in IntelliSense, error messages, and type hints—making your code self-documenting.
Mixing With Optional and Rest Elements
Labels work seamlessly with optional (?) and rest (...) elements:
type User = [id: number, name: string, email?: string];
type Args = [command: string, ...flags: string[]];
Why Use Named Labels?
Named labels shine when:
- Tuples represent structured data (coordinates, responses, database rows)
- You're returning multiple values from a function
- Tuple positions have semantic meaning that's not obvious from types alone
Key Takeaway: Named tuple labels add zero-cost documentation directly into tuple types using label: type notation, making position-based data structures self-explanatory without changing runtime behavior.