Labels in Function Signatures
What you'll learn: How to use named tuple labels to make rest parameters and return types self-documenting and easier to understand.
When you use rest parameters or return tuples from functions, TypeScript lets you attach labels to each position. These labels don't affect runtime behavior or type checking—they're purely documentation that appears in tooltips and IntelliSense.
Why Labels Matter
Imagine a function that returns a tuple:
function getCoordinates(): [number, number, number] {
return [10, 20, 5];
}
Which number is latitude? Which is longitude? Which is altitude? Without reading documentation or implementation, you can't tell.
Adding Labels to Tuple Return Types
You can name each position:
function getCoordinates(): [latitude: number, longitude: number, altitude: number] {
return [10, 20, 5];
}
const [lat, lon, alt] = getCoordinates();
Now when you hover over getCoordinates, you see exactly what each number represents. It's like labeling the outputs of a machine—you instantly know which dial is which.
Labels in Rest Parameters
Rest parameters also benefit from labels:
function createURL(base: string, ...segments: [path: string, query: string]) {
const [path, query] = segments;
return `${base}/${path}?${query}`;
}
createURL("https://api.com", "users", "active=true");
The labels path and query clarify what those rest parameter positions mean without requiring separate parameter names.
Mixing Labeled and Unlabeled Elements
You can mix labeled and unlabeled positions, though consistency is clearer:
function process(): [status: number, string, data: unknown] {
return [200, "OK", { id: 1 }];
}
Key Takeaway: Named tuple labels act as inline documentation for function signatures, making rest parameters and tuple returns self-explanatory without changing runtime behavior or type safety.