Challenge - 5 Problems
Tuple Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this tuple length check?
Consider this TypeScript code that defines a tuple and checks its length.
What will be the output when this code runs?
What will be the output when this code runs?
Typescript
const myTuple: [string, number, boolean] = ['hello', 42, true]; console.log(myTuple.length);
Attempts:
2 left
💡 Hint
Remember tuples are fixed-length arrays in TypeScript.
✗ Incorrect
The tuple has exactly three elements, so its length property is 3.
❓ Predict Output
intermediate2:00remaining
What is the output when accessing a tuple element?
Given this tuple definition and access, what will be printed?
Typescript
const coords: [number, number] = [10, 20]; console.log(coords[1]);
Attempts:
2 left
💡 Hint
Tuple elements are accessed by zero-based index.
✗ Incorrect
coords[1] accesses the second element, which is 20.
🔧 Debug
advanced2:00remaining
What error does this tuple assignment cause?
This code tries to assign a tuple to a variable. What error will TypeScript report?
Typescript
let user: [string, number]; user = ['Alice', '25'];
Attempts:
2 left
💡 Hint
Check the types of each tuple element carefully.
✗ Incorrect
The second element should be a number, but '25' is a string.
📝 Syntax
advanced2:00remaining
Which option correctly defines a tuple type with optional elements?
Which of these tuple type definitions is valid TypeScript syntax for a tuple with an optional boolean as the third element?
Attempts:
2 left
💡 Hint
Optional elements in tuples use a question mark after the type.
✗ Incorrect
Only option B correctly places the question mark after the type to mark the third element optional.
🚀 Application
expert2:00remaining
What is the type of the variable 'result' after this tuple manipulation?
Given this TypeScript code, what is the type of 'result'?
Typescript
const data: [number, string, boolean] = [1, 'ok', true]; const result = data.slice(0, 2);
Attempts:
2 left
💡 Hint
Array.slice returns a new array, not a tuple.
✗ Incorrect
The slice method returns a normal array with elements of the tuple types included, so the type is Array.