

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Tuples
incomplete
2: Readonly
incomplete
3: Tuples vs. Objects
incomplete
4: Destructuring Tuples
incomplete
5: Named Tuples
incomplete
6: Optional Elements in Tuples
incomplete
7: Tuple Rest Elements
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Sometimes tuples are also useful when you want to return multiple values from a function (which is impossible in JS/TS), but you don't want to create a new object type just to do so. A tuple, along with destructuring, is a handy way to return "positional" data.
function getName(fullName: string): [string, string] {
const parts = fullName.split(" ");
return [parts[0], parts[1]];
}
const [firstName, lastName] = getName("Frodo Baggins");
There's nothing stopping you from destructuring nested tuples and objects all at once. Use this example to answer the question:
type UserWithAddress = [string, { city: string; country: string }];
const userData: UserWithAddress = [
"Aragorn",
{ city: "Minas Tirith", country: "Gondor" },
];
const [userName, { city, country }] = userData;
console.log(city);
// ?