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?
AAt the beginning
BIn the middle
CAt the end
DAnywhere
✗ 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?
AA tuple with only numbers
BA tuple with any types
CA tuple with only strings
DA tuple starting with a number, followed by zero or more strings
✗ 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?
AA single boolean
BAn array of booleans
CA tuple of booleans
DA number
✗ Incorrect
The rest element collects multiple booleans into an array.
Can you have multiple rest elements in a single tuple type?
ANo, only one rest element at the end
BYes, anywhere
CYes, but only at the start
DYes, but only in the middle
✗ 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?
A[number, ...string[]]
B[string, number, ...boolean, string]
C[...string[], number]
D[...number[], ...string[]]
✗ 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.
Practice
(1/5)
1.
What is the main purpose of using rest elements in TypeScript tuples?
easy
A. To define the exact fixed length of a tuple
B. To make tuples immutable
C. To allow tuples to contain only one type of element
D. To collect multiple elements into a single tuple part
Solution
Step 1: Understand rest elements in tuples
Rest elements gather multiple items into one part of a tuple, allowing flexible length.
Step 2: Identify the purpose
This means rest elements help collect many elements into one part of a tuple, not fix length or types.
Final Answer:
To collect multiple elements into a single tuple part -> Option D
Quick Check:
Rest elements collect multiple items [OK]
Hint: Rest elements gather extra items [OK]
Common Mistakes:
Thinking rest elements fix tuple length
Using ... without array type
Confusing rest elements with tuple immutability
2.
Which of the following is the correct syntax for a tuple with a rest element in TypeScript?
type MyTuple = [number, ...string[]];
easy
A. type MyTuple = [number, string...];
B. type MyTuple = [...number, string[]];
C. type MyTuple = [number, ...string[]];
D. type MyTuple = [number, ...string];
Solution
Step 1: Check rest element syntax
Rest elements use three dots (...) before a type array, placed at the end.
Step 2: Validate each option
type MyTuple = [number, ...string[]]; correctly uses [...string[]] after number. Others misuse syntax or placement.
Final Answer:
type MyTuple = [number, ...string[]]; -> Option C
Quick Check:
Rest element syntax = ...type[] at end [OK]
Hint: Rest element syntax: ...type[] at tuple end [OK]
Common Mistakes:
Placing rest element in the middle
Omitting brackets around rest type
Using ...type instead of ...type[]
3.
What will be the output of the following TypeScript code?
The tuple starts with a string, then any number of numbers via rest element.
Step 2: Count elements in example
example has 'start', 1, 2, 3 which is 4 elements total.
Final Answer:
4 -> Option B
Quick Check:
Tuple length counts all elements including rest [OK]
Hint: Tuple length includes rest elements count [OK]
Common Mistakes:
Counting only fixed elements ignoring rest
Expecting error due to rest element
Confusing tuple length with rest array length
4.
Identify the error in this tuple type declaration:
type WrongTuple = [...string, number];
medium
A. Rest element must be an array type
B. Rest element must be at the end of the tuple
C. Tuple cannot have more than two elements
D. Number type cannot be used in tuples
Solution
Step 1: Check rest element syntax
Rest elements must use an array or tuple type after ..., like ...string[].
Step 2: Analyze given tuple
Here, ...string uses primitive string, not array/tuple type, invalid syntax.
Final Answer:
Rest element must be an array type -> Option A
Quick Check:
Rest element type = array/tuple only [OK]
Hint: Rest element requires array/tuple type [OK]
Common Mistakes:
Using ... with non-array type like ...string
Thinking rest element can't be array type
Confusing tuple length limits
5.
Given the tuple type type Data = [string, ...number[], boolean], which of the following assignments is valid?
A) const a: Data = ['hello', 1, 2, true];
B) const b: Data = ['hello', 42];
C) const c: Data = ['hello', 1, 2, 3, false];
D) const d: Data = ['hello', 1, false, 2];
hard
A. Only A and C are valid
B. Only B and D are valid
C. Only C is valid
D. Only D is valid
Solution
Step 1: Understand tuple with rest element in middle
Type [string, ...number[], boolean] means string first, then any numbers, then boolean last.
Step 2: Check each assignment
A) Ends with true (boolean), numbers in middle: ['hello',1,2,true] valid. B) Missing boolean after numbers, only string and number: invalid. C) Ends with false (boolean), numbers in middle: valid. D) Boolean before number (false before 2): invalid order.
Final Answer:
Only A and C are valid -> Option A
Quick Check:
Rest numbers must be between string and boolean [OK]
Hint: Rest numbers must be between first string and last boolean [OK]
Common Mistakes:
Placing boolean before rest numbers
Omitting rest numbers entirely
Assuming rest can be anywhere
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.