How do labeled tuple elements improve readability and editor tooling compared to unlabeled tuples?
Short answer
Adding a name before each element's type (like `[x: number, y: number]`) doesn't change runtime behavior at all, but significantly improves editor autocomplete/hover tooltips and function signature readability — especially valuable for tuples used as function parameter lists or…
Advertisement
AModel answer
Adding a name before each element's type (like `[x: number, y: number]`) doesn't change runtime behavior at all, but significantly improves editor autocomplete/hover tooltips and function signature readability — especially valuable for tuples used as function parameter lists or return values with multiple positions.
function move([deltaX, deltaY]: [x: number, y: number]) {
console.log(`Moving by ${deltaX}, ${deltaY}`);
}
// hovering the tuple type in an editor now shows 'x' and 'y' labels instead of just 'number, number'Real-world example
Improving the developer experience of a utility function's tuple-typed return value, like a custom useToggle() hook returning [value, toggle].
Likely follow-ups
- Do labeled tuple elements require ALL elements in the tuple to be labeled, or can you mix labeled and unlabeled?
Related topics