

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
To be fair, position-based access isn't very descriptive. Luckily, you can label tuple elements (sometimes called "named tuples"). So, instead of this:
type UserData = [string, number, boolean];
We can do this:
type UserDataLabeled = [name: string, age: number, isAdmin: boolean];
Labels make your code more "self-documenting".
You might hear people say "there's no such thing as self-documenting code". Those people are just mad because they write terrible code. If you name things well and keep things simple, you'll still need comments occasionally, but you won't need them as often.
When you hover over a variable in your editor, you'll see names instead of just positions:
// Your editor shows the full type:
// [name: string, age: number, isAdmin: boolean]
function getUser(): UserDataLabeled {
return ["Frodo", 33, false];
}
The labels are quite literally just names for the TypeScript tooling, they don't change how the values are accessed. Say I have a named tuple like this:
const user: [name: string, age: number] = ["Bilbo", 111];
And then I try to destructure in reverse order:
const [age, name] = user;
console.log(age); // "Bilbo"
console.log(name); // 111
The variable names I choose when destructuring don't matter: only the positions do.
The formatTicket function accepts a Ticket tuple and returns a formatted string describing the ticket.
Expected format: #1 Your app stinks! [WONTFIX]