Recall & Review
beginner
What is a rest element in a tuple in TypeScript?
A rest element in a tuple allows you to capture multiple elements into a single array within the tuple. It uses the syntax
...name to gather remaining elements.Click to reveal answer
beginner
How do you declare a tuple with a rest element that captures numbers in TypeScript?
You can declare it like this:
type Numbers = [string, ...number[]]; This means the first element is a string, followed by zero or more numbers.Click to reveal answer
intermediate
Can rest elements in tuples be placed anywhere other than the end?
No, rest elements must be the last element in a tuple type. This is because they capture all remaining elements.
Click to reveal answer
beginner
What is the type of a rest element in a tuple?
The rest element type is always an array type. For example,
...number[] means the rest elements are numbers collected into an array.Click to reveal answer
beginner
Example: What is the type of <code>const t: [string, ...boolean[]]</code>?It means
t is a tuple where the first element is a string, followed by zero or more boolean values.Click to reveal answer
In TypeScript, where must the rest element appear in a tuple type?
✗ Incorrect
Rest elements must always be the last element in a tuple type to capture all remaining elements.
What does the tuple type
[number, ...string[]] represent?✗ Incorrect
The first element is a number, and the rest are strings collected into an array.
What type is the rest element
...boolean[] in a tuple?✗ Incorrect
The rest element collects multiple booleans into an array.
Can you have multiple rest elements in a single tuple type?
✗ Incorrect
Only one rest element is allowed and it must be at the end of the tuple.
Which of these is a valid tuple type with a rest element?
✗ Incorrect
Only option C has the rest element at the end and is valid syntax.
Explain what a rest element in a tuple is and how it is used in TypeScript.
Think about how you gather leftover items in a list.
You got /4 concepts.
Write a tuple type with a string first element and any number of boolean elements after it.
Use ...boolean[] after the string.
You got /4 concepts.