Optional elements in tuples let you create fixed-size lists where some items can be left out. This helps when you want to allow some parts to be missing but keep the order.
Optional elements in tuples in Typescript
type MyTuple = [string, number?, boolean?];
Use a question mark ? after the type to mark an element as optional.
Optional elements must come after required elements in the tuple.
type PersonInfo = [string, number?, boolean?]; const a: PersonInfo = ["Alice"]; const b: PersonInfo = ["Bob", 30]; const c: PersonInfo = ["Carol", 25, true];
type Coordinates = [number, number, number?]; const point1: Coordinates = [10, 20]; const point2: Coordinates = [10, 20, 30];
This program defines a tuple with optional elements and prints each part, showing how missing values are handled.
type User = [string, number?, boolean?]; function printUser(user: User) { const [name, age, isAdmin] = user; console.log(`Name: ${name}`); if (age !== undefined) { console.log(`Age: ${age}`); } else { console.log("Age: not provided"); } if (isAdmin !== undefined) { console.log(`Admin: ${isAdmin}`); } else { console.log("Admin: not provided"); } } printUser(["Alice"]); printUser(["Bob", 28]); printUser(["Carol", 35, true]);
Optional elements must be at the end of the tuple; you cannot have a required element after an optional one.
When accessing optional elements, check if they are undefined to avoid errors.
Optional elements in tuples let you have fixed order but allow some items to be missing.
Mark optional elements with ? after the type.
Always put optional elements after required ones.
Practice
What does the ? symbol mean when used in a tuple type like [string, number?] in TypeScript?
Solution
Step 1: Understand the
The?in tuple types?after a type in a tuple means that element is optional and can be omitted.Step 2: Compare with other options
Options B, C, and D describe different concepts unrelated to optional tuple elements.Final Answer:
It marks the element as optional, so it may be missing. -> Option BQuick Check:
Optional element = marked with ? [OK]
- Thinking ? means the element is required
- Confusing ? with union types
- Assuming ? makes element readonly
Which of the following is the correct syntax for a tuple with a required string and an optional number in TypeScript?
Solution
Step 1: Identify correct order and optional placement
Optional elements must come after required ones. Sostringrequired first, then optionalnumber.Step 2: Check each option
[string, number?] has requiredstringthen optionalnumber. Others put optional before required or both optional incorrectly.Final Answer:
[string, number?] -> Option AQuick Check:
Required first, optional after = [string, number?] [OK]
- Placing optional element before required one
- Marking required element as optional
- Using wrong syntax like ? before type
What will be the output of this TypeScript code?
type MyTuple = [string, number?]; const a: MyTuple = ['hello']; const b: MyTuple = ['world', 42]; console.log(a.length, b.length);
Solution
Step 1: Understand tuple with optional element
Tuple[string, number?]means second element can be missing. Soahas length 1,bhas length 2.Step 2: Check console.log output
Logginga.lengthis 1,b.lengthis 2, so output is "1 2".Final Answer:
1 2 -> Option CQuick Check:
Optional element missing means shorter tuple length [OK]
- Assuming tuple length is fixed always
- Expecting error when optional element missing
- Confusing array length with tuple length
Identify the error in this tuple type declaration:
type Data = [number?, string];
Solution
Step 1: Analyze tuple element order
Optional elements must come after all required elements in tuples.Step 2: Check given tuple
Here,number?is optional but placed before requiredstring, which is invalid.Final Answer:
Optional element cannot come before required element. -> Option DQuick Check:
Optional elements must be last in tuple [OK]
- Placing optional element before required one
- Thinking optional elements are not allowed
- Misusing ? syntax
Given this tuple type with multiple optional elements:
type Config = [string, number?, boolean?];
Which of these assignments is invalid?
const c1: Config = ['mode']; const c2: Config = ['mode', 10]; const c3: Config = ['mode', undefined, true]; const c4: Config = ['mode', true];
Solution
Step 1: Understand tuple element types and order
Tuple is[string, number?, boolean?]. The second element must be a number or omitted, third a boolean or omitted.Step 2: Check each assignment
c1has only required string - valid.c2has string and number - valid.c3has string, undefined (allowed for optional), and boolean - valid.c4has string and boolean in second position - invalid because second element must be number or omitted.
Final Answer:
const c4: Config = ['mode', true]; -> Option AQuick Check:
Optional elements must match declared types in order [OK]
- Putting boolean in number position
- Using undefined incorrectly
- Ignoring element order in tuples
Ready to prove it?
Pass the test and this pattern moves unknown
