A tuple is a fixed-length array where each element has a specific type. It allows you to store multiple values of different types in a single variable.
Click to reveal answer
beginner
How do you declare a tuple with a fixed length of 3 elements: a string, a number, and a boolean?
You declare it like this: <pre>let myTuple: [string, number, boolean];</pre> This means the tuple must have exactly three elements in that order and type.
Click to reveal answer
intermediate
Can you change the length of a tuple after it is declared?
No, tuples have a fixed length defined by their type. You cannot add or remove elements beyond the declared length without causing a type error.
Click to reveal answer
beginner
What happens if you try to assign a wrong type to a tuple element?
TypeScript will show a type error because each position in the tuple expects a specific type. For example, assigning a string to a number position is not allowed.
Click to reveal answer
beginner
Example: How to access the second element of a tuple [string, number, boolean]?
You access it by index like an array:
myTuple[1]
which will give you the number value because indexes start at 0.
Click to reveal answer
What does the tuple type [string, number] mean?
AA function returning string and number
BAn array with exactly two elements: first a string, second a number
CAn object with string and number properties
DAn array with any number of strings and numbers
✗ Incorrect
A tuple [string, number] means exactly two elements: first a string, second a number.
Can you assign ["hello", 42, true] to a variable typed as [string, number, boolean]?
ANo, because booleans are not allowed
BNo, because the length is wrong
CYes, but only if you use type assertion
DYes, because the types and order match
✗ Incorrect
The tuple [string, number, boolean] matches the value ["hello", 42, true] exactly in type and order.
What happens if you try to push an extra element to a tuple?
ATypeScript will give a type error
BThe tuple will accept it without error
CThe tuple will convert to an array
DThe extra element will be ignored
✗ Incorrect
Tuples have fixed length, so pushing an extra element causes a type error.
How do you declare a tuple with two elements: a boolean and a string?
Alet t: [boolean, string];
Blet t: (boolean | string)[];
Clet t: boolean & string;
Dlet t: {boolean, string};
✗ Incorrect
The correct tuple syntax is [boolean, string] for fixed length and types.
Which index accesses the first element of a tuple?
A1
B-1
C0
DIt depends on the tuple
✗ Incorrect
Tuple elements are accessed by zero-based index, so the first element is at index 0.
Explain what a tuple with fixed length and types is in TypeScript and why it is useful.
Think about a small box with fixed slots for different items.
You got /4 concepts.
How do you declare and use a tuple with three elements: string, number, and boolean? Give an example.
Remember tuples are like arrays but with fixed types and length.
You got /4 concepts.
Practice
(1/5)
1. What is a tuple in TypeScript?
easy
A. A fixed-length array with specific types for each element
B. A variable-length array with any types
C. A function that returns multiple values
D. A type of object with dynamic keys
Solution
Step 1: Understand tuple definition
A tuple is an array with a fixed number of elements where each element has a specific type.
Step 2: Compare with other options
Options A, B, and C describe other concepts like objects, variable arrays, or functions, not tuples.
Final Answer:
A fixed-length array with specific types for each element -> Option A
2. Which of the following is the correct syntax to declare a tuple with a string and a number in TypeScript?
easy
A. let tuple: [string; number] = ['hello', 5];
B. let tuple: (string, number) = ['hello', 5];
C. let tuple: {string, number} = ['hello', 5];
D. let tuple: [string, number] = ['hello', 5];
Solution
Step 1: Recall tuple syntax
Tuples use square brackets with types separated by commas: [type1, type2].
Step 2: Check each option
let tuple: [string, number] = ['hello', 5]; uses correct syntax. Options B, C, and D use invalid tuple syntax.
Final Answer:
let tuple: [string, number] = ['hello', 5]; -> Option D
Quick Check:
Tuple syntax = square brackets with comma-separated types [OK]
Hint: Use square brackets with commas for tuple types [OK]
Common Mistakes:
Using parentheses instead of brackets
Using curly braces instead of brackets
Using semicolons instead of commas
3. What will be the output of the following TypeScript code?
let data: [string, number] = ['age', 30];
console.log(data[0]);
console.log(data[1]);
medium
A. 30\nage
B. undefined\nundefined
C. age\n30
D. Error: Type mismatch
Solution
Step 1: Understand tuple assignment
The tuple 'data' is assigned ['age', 30], so data[0] is 'age' and data[1] is 30.
Step 2: Check console.log outputs
console.log(data[0]) prints 'age', console.log(data[1]) prints 30, so output is two lines: 'age' then '30'.
Final Answer:
age
30 -> Option C
Quick Check:
Tuple elements print in order: string then number [OK]
Hint: Tuple elements print in order by index [OK]
Common Mistakes:
Swapping the order of elements
Expecting an error due to types
Confusing tuple with object properties
4. Identify the error in this TypeScript tuple declaration:
let info: [string, number] = ['John', '25'];
medium
A. The second element should be a number, not a string
B. The tuple length is incorrect
C. The first element should be a number
D. No error, this is valid
Solution
Step 1: Check tuple types
The tuple expects [string, number], so first element string 'John' is correct, second element must be a number.
Step 2: Identify type mismatch
The second element is '25' (a string), which mismatches the expected number type.
Final Answer:
The second element should be a number, not a string -> Option A
Quick Check:
Tuple types must match exactly [OK]
Hint: Check each tuple element type carefully [OK]
Common Mistakes:
Ignoring type mismatch in tuple elements
Assuming string '25' is a number
Thinking tuple length is wrong
5. Given the tuple type [string, number, boolean], which of the following assignments is valid in TypeScript?
hard
A. let t: [string, number, boolean] = ['hello', true, 42];
B. let t: [string, number, boolean] = ['hello', 42, true];
C. let t: [string, number, boolean] = ['hello', 42];
D. let t: [string, number, boolean] = ['hello', 42, 'true'];
Solution
Step 1: Check tuple length and types
The tuple requires exactly three elements: string, number, boolean in that order.
Step 2: Evaluate each option
let t: [string, number, boolean] = ['hello', 42, true]; matches all types and length correctly. let t: [string, number, boolean] = ['hello', true, 42]; swaps number and boolean types. let t: [string, number, boolean] = ['hello', 42]; misses the third element. let t: [string, number, boolean] = ['hello', 42, 'true']; uses a string instead of boolean.
Final Answer:
let t: [string, number, boolean] = ['hello', 42, true]; -> Option B
Quick Check:
Tuple must match length and types exactly [OK]
Hint: Match tuple length and types exactly in order [OK]
Common Mistakes:
Changing order of types
Omitting elements
Using wrong types for elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.