Destructuring Tuples
What
Destructuring allows you to unpack values from a tuple directly into distinct variables.
How
You use the array destructuring syntax to assign names to the positional values:
function getName(fullName: string): [string, string] {
const parts = fullName.split(" ");
return [parts[0], parts[1]];
}
const [firstName, lastName] = getName("Frodo Baggins");
Why
Tuples are often used to return multiple values from a function without the overhead of creating a new object type. Destructuring makes this pattern extremely clean by allowing you to name the returned values immediately in the calling code.
Summary
Destructuring provides a concise way to handle positional data returned from functions.