0
0
Typescriptprogramming~5 mins

Rest elements in tuples in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AAt the beginning
BIn the middle
CAt the end
DAnywhere
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
What type is the rest element ...boolean[] in a tuple?
AA single boolean
BAn array of booleans
CA tuple of booleans
DA number
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
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[]]
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.