Course contentsShow
← TypeScript
Lesson 123 of 1,7963. Basic TypesFree lesson

Labeled Tuple Elements

Add descriptive labels for clarity: [name: string, age: number] improves readability without changing runtime behavior.

Labeled Tuple Elements

What you'll learn: How to add descriptive labels to tuple positions for better readability without changing how your code runs.

Why Label Your Tuples?

Imagine you see this tuple type: [string, number, boolean]. What do these values represent? You'd have to guess or dig through documentation. Labeled tuple elements let you answer that question in the type itself.

Adding Labels for Clarity

You can attach a label to each position in a tuple using the syntax name: type:

// Without labels - unclear
type User = [string, number];

// With labels - self-documenting
type LabeledUser = [name: string, age: number];

Both versions create the exact same runtime behavior. The labels are purely for human readers—they don't change how JavaScript executes your code.

Using Labeled Tuples

type Coordinate = [x: number, y: number];

const point: Coordinate = [10, 20];

// Access works the same way
const xValue = point[0]; // Still accessed by index

The labels appear in your IDE's autocomplete and error messages, making your code easier to understand at a glance. When you hover over Coordinate in your editor, you'll see [x: number, y: number] instead of just [number, number].

Labels Are Documentation

Think of labels like variable names in a function signature—they don't change what the function does, but they tell you what each parameter means. Labeled tuples serve the same purpose: they make your types self-explanatory.

Key Takeaway: Labeled tuple elements ([name: string, age: number]) add descriptive names to tuple positions, improving code readability without affecting runtime behavior—they're pure documentation that lives in your type system.