📖 Scenario: Imagine you are creating a simple system to store information about books. Some books have a subtitle, but not all. You want to use a tuple to store the title and optionally the subtitle.
🎯 Goal: Build a TypeScript tuple that can hold a book's title and optionally its subtitle using optional elements in tuples.
📋 What You'll Learn
Create a tuple type called Book with a required string for the title and an optional string for the subtitle.
Create a variable called book1 of type Book with only the title.
Create a variable called book2 of type Book with both title and subtitle.
Print both book1 and book2 to the console.
💡 Why This Matters
🌍 Real World
Optional tuple elements help model data where some parts may or may not be present, like subtitles in book titles.
💼 Career
Understanding optional tuple elements is useful for TypeScript developers working with flexible data structures in real-world applications.
Progress0 / 4 steps
1
Create the tuple type with an optional element
Create a tuple type called Book with a required string for the title and an optional string for the subtitle using the syntax [string, string?].
Typescript
Hint
Use string? to mark the subtitle as optional in the tuple type.
2
Create a tuple variable with only the title
Create a variable called book1 of type Book and assign it a tuple with only the title 'The Great Gatsby'.
Typescript
Hint
Assign a tuple with one string element to book1.
3
Create a tuple variable with title and subtitle
Create a variable called book2 of type Book and assign it a tuple with the title 'The Hobbit' and subtitle 'There and Back Again'.
Typescript
Hint
Assign a tuple with two string elements to book2.
4
Print both tuples to the console
Write two console.log statements to print book1 and book2 to the console.
Typescript
Hint
Use console.log(book1); and console.log(book2); to show the tuples.
Practice
(1/5)
1.
What does the ? symbol mean when used in a tuple type like [string, number?] in TypeScript?
easy
A. It means the element is required and must always be present.
B. It marks the element as optional, so it may be missing.
C. It indicates the element can be any type.
D. It makes the element readonly and unchangeable.
Solution
Step 1: Understand the ? in tuple types
The ? after a type in a tuple means that element is optional and can be omitted.
Step 2: Compare with other options
Options B, C, and D describe different concepts unrelated to optional tuple elements.
Final Answer:
It marks the element as optional, so it may be missing. -> Option B
Quick Check:
Optional element = marked with ? [OK]
Hint: Optional tuple elements have ? after type, meaning may be missing [OK]
Common Mistakes:
Thinking ? means the element is required
Confusing ? with union types
Assuming ? makes element readonly
2.
Which of the following is the correct syntax for a tuple with a required string and an optional number in TypeScript?
easy
A. [string, number?]
B. [string?, number]
C. [number?, string]
D. [string?, number?]
Solution
Step 1: Identify correct order and optional placement
Optional elements must come after required ones. So string required first, then optional number.
Step 2: Check each option
[string, number?] has required string then optional number. Others put optional before required or both optional incorrectly.
Final Answer:
[string, number?] -> Option A
Quick Check:
Required first, optional after = [string, number?] [OK]
Hint: Put optional elements after required ones in tuple [OK]