Recall & Review
beginner
What is a tuple type in TypeScript?
A tuple type in TypeScript is an array with a fixed number of elements where each element can have a different type. It defines the exact types and order of elements.
Click to reveal answer
beginner
How do you define a tuple type with a string and a number in TypeScript?
You define it like this: <code>let example: [string, number];</code> This means the first element must be a string and the second a number.Click to reveal answer
intermediate
Can tuple elements be optional in TypeScript? How?
Yes, tuple elements can be optional by adding a question mark after the type. Example: <code>let example: [string, number?];</code> means the second element can be a number or undefined.Click to reveal answer
beginner
What happens if you try to assign wrong types to a tuple in TypeScript?
TypeScript will show an error because tuples expect specific types in a fixed order. For example, assigning a number where a string is expected will cause a type error.
Click to reveal answer
advanced
How can you use rest elements in tuple types?
You can use rest elements to allow a variable number of elements of the same type at the end. Example: <code>let example: [string, ...number[]];</code> means one string followed by any number of numbers.Click to reveal answer
How do you declare a tuple with a boolean and a string in TypeScript?
✗ Incorrect
Option A correctly declares a tuple with a boolean followed by a string. Option B is an array of boolean or string, not a tuple.
What is true about tuple types in TypeScript?
✗ Incorrect
Tuples have fixed length and each position has a specific type. Arrays can have variable length and usually one type.
How do you make the last element of a tuple a variable number of strings?
✗ Incorrect
Option C uses rest syntax to allow any number of strings after a number.
What error occurs if you assign
[42, 'hello'] to let t: [string, number]?✗ Incorrect
The tuple expects a string first, but 42 is a number, so TypeScript shows a type error.
Can tuple types have optional elements in TypeScript?
✗ Incorrect
You can mark tuple elements optional with a question mark, e.g.,
[string, number?].Explain what a tuple type is in TypeScript and how it differs from a regular array.
Think about how many elements and what types each element can have.
You got /4 concepts.
Describe how to define a tuple with optional and rest elements in TypeScript.
Consider how to allow some elements to be missing or repeated.
You got /3 concepts.