0
0
Typescriptprogramming~20 mins

Tuple type definition in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tuple Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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?
Typescript
const myTuple: [string, number, boolean] = ['hello', 42, true];
console.log(myTuple.length);
A3
B2
CError: Tuple length not accessible
Dundefined
Attempts:
2 left
💡 Hint
Remember tuples are fixed-length arrays in TypeScript.
Predict Output
intermediate
2: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]);
Aundefined
B10
C20
DError: Index out of bounds
Attempts:
2 left
💡 Hint
Tuple elements are accessed by zero-based index.
🔧 Debug
advanced
2: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'];
ATuple must have exactly 3 elements.
BNo error, assignment is valid.
CType 'number' is not assignable to type 'string'.
DType 'string' is not assignable to type 'number'.
Attempts:
2 left
💡 Hint
Check the types of each tuple element carefully.
📝 Syntax
advanced
2: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?
Atype MyTuple = [string, number?, boolean];
Btype MyTuple = [string, number, boolean?];
Ctype MyTuple = [string?, number, boolean];
Dtype MyTuple = [string, number, ?boolean];
Attempts:
2 left
💡 Hint
Optional elements in tuples use a question mark after the type.
🚀 Application
expert
2: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);
AArray<number | string>
B[number, string]
C[number, string, boolean]
DArray<number | string | boolean>
Attempts:
2 left
💡 Hint
Array.slice returns a new array, not a tuple.