Lesson 50 of 80
Naming a tuple's positions — `[min: number, max: number]` instead of `[number, number]` — for editor hints and self-documenting signatures, with zero effect on runtime behavior.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
The tuples lesson already covered that a tuple's fixed-length, positional typing is what makes [value, setter]-style destructuring work correctly. What it deliberately left for later: [string, number] tells a reader what type each position holds, but not what it means. Two same-shaped tuples used for completely different things — a [string, number] name/age pair versus a [string, number] min/max range — look identical in a hover tooltip. Labeled tuple elements close that gap:
type NameAge = [name: string, age: number];
type Range = [min: number, max: number];
The labels (name, age, min, max) are pure documentation. They show up in editor autocomplete, in hover tooltips, and in error messages — but they compile to exactly the same runtime array as the unlabeled version, and you still destructure by position, not by label.
function clamp(value: number, [min, max]: [min: number, max: number]): number {
return Math.min(Math.max(value, min), max);
}
clamp(15, [0, 10]); // 10 — destructured by position, same as always
Compare the signature to the unlabeled version, [number, number] — a reader (or an editor's parameter hint popup) has no way to tell which number is the floor and which is the ceiling without opening the function body. [min: number, max: number] answers that question at the call site, in the same place TypeScript already shows you the type.
const r: Range = [0, 10]; r.min is a compile error — labeled tuples are still accessed by numeric index (r[0]) or positional destructuring, never r.min. If you actually want named-property access, you want an object type, not a tuple.[10, 0] (max before min, despite the labels saying otherwise) compiles without complaint — TypeScript checks that the values are numbers in the right positions, not that they satisfy whatever the label's name implies.[name: string, age?: number] and [first: string, ...rest: number[]] both label-annotate perfectly normally; labeling doesn't require giving up any other tuple feature."Given a function with a [number, number] tuple parameter, how would you make its meaning clear without changing its runtime shape or forcing callers into an object?" is a fair follow-up once someone's already demonstrated they understand tuples — labeled elements are the answer, and knowing they're erased at runtime (unlike renaming to an object type, which would change the call site) shows the deeper understanding.