Challenge - 5 Problems
Tuple Rest Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of tuple with rest element in function parameter
What is the output of this TypeScript code when calling
logTuple([1, 2, 3, 4])?Typescript
function logTuple([first, ...rest]: [number, ...number[]]) { console.log(first, rest.length); } logTuple([1, 2, 3, 4]);
Attempts:
2 left
💡 Hint
Remember that the rest element collects all remaining items after the first.
✗ Incorrect
The first element is 1, and the rest collects [2, 3, 4], so rest.length is 3.
❓ Predict Output
intermediate2:00remaining
Length of tuple with rest element
What is the length of the tuple
type T = [string, ...number[]] when assigned const x: T = ['hello', 1, 2, 3]?Typescript
type T = [string, ...number[]]; const x: T = ['hello', 1, 2, 3]; console.log(x.length);
Attempts:
2 left
💡 Hint
The rest element allows any number of elements after the first.
✗ Incorrect
The tuple has one string followed by any number of numbers, so length is 4 here.
🔧 Debug
advanced2:00remaining
Identify the error with rest element in tuple type
Why does this TypeScript code cause an error?
Typescript
type T = [...number[], string]; const x: T = [1, 2, 3, 'end'];
Attempts:
2 left
💡 Hint
Check the position of the rest element in the tuple type.
✗ Incorrect
In TypeScript, rest elements in tuple types must be last. Here, the rest is first, causing an error.
🚀 Application
advanced2:00remaining
Function parameter with tuple rest element
Which function signature correctly accepts a tuple with a string followed by any number of booleans?
Attempts:
2 left
💡 Hint
Rest element must be last and match the type of remaining elements.
✗ Incorrect
Option A correctly defines a tuple with a string first and any number of booleans after.
🧠 Conceptual
expert3:00remaining
Rest elements and tuple inference behavior
Given
const t = [1, 2, 3] as const; and type T = [number, ...number[]];, what is the inferred type of t and does it satisfy T?Typescript
const t = [1, 2, 3] as const; type T = [number, ...number[]];
Attempts:
2 left
💡 Hint
Consider how
as const affects tuple types and readonly status.✗ Incorrect
The
as const makes t a readonly tuple [1, 2, 3], which matches the pattern [number, ...number[]].