Bird
Raised Fist0
Typescriptprogramming~10 mins

Rest elements in tuples in Typescript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Rest elements in tuples
Define tuple with rest element
↓
Assign values to tuple
↓
Rest element collects remaining items
↓
Use tuple with fixed + rest elements
This flow shows how a tuple with a rest element collects remaining items into an array after fixed elements.
Execution Sample
Typescript
const tuple: [number, ...string[]] = [1, 'a', 'b', 'c'];
console.log(tuple[0]);
console.log(tuple.slice(1));
Defines a tuple with a number followed by any number of strings, then logs the first element and the rest.
Execution Table
StepActionTuple ContentRest Element ContentOutput
1Define tuple type [number, ...string[]][][]
2Assign [1, 'a', 'b', 'c'] to tuple[1, 'a', 'b', 'c']['a', 'b', 'c']
3Access tuple[0][1, 'a', 'b', 'c']['a', 'b', 'c']1
4Access tuple.slice(1)[1, 'a', 'b', 'c']['a', 'b', 'c']['a', 'b', 'c']
5End of execution[1, 'a', 'b', 'c']['a', 'b', 'c']
💡 All elements assigned; rest element collects all strings after first number.
Variable Tracker
VariableStartAfter AssignmentFinal
tupleundefined[1, 'a', 'b', 'c'][1, 'a', 'b', 'c']
rest elementsundefined['a', 'b', 'c']['a', 'b', 'c']
Key Moments - 2 Insights
Why does the rest element collect multiple items instead of just one?
Because in the tuple type [number, ...string[]], the ...string[] means any number of strings after the first number, so all remaining items are grouped into an array as the rest element (see execution_table step 2).
Can the rest element be placed anywhere other than the end?
No, rest elements in tuples must be last to collect all remaining items. This is why the tuple type has the rest element at the end (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 3, what is the value of tuple[0]?
A'a'
B1
C['a', 'b', 'c']
Dundefined
💡 Hint
Check the 'Output' column at step 3 in execution_table.
At which step does the rest element collect the strings 'a', 'b', and 'c'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Rest Element Content' column in execution_table.
If the tuple was defined as [number, string, ...boolean[]], what would the rest element collect?
AOnly the first boolean
BAll strings after the first number
CAll booleans after the first number and string
DNo elements, rest must be last
💡 Hint
Rest element collects all remaining items of its type after fixed elements (see concept_flow).
Concept Snapshot
Tuple with rest element syntax: [fixed1, fixed2, ...restType[]]
Rest element collects all remaining items into an array
Rest element must be last in tuple
Access fixed elements by index
Rest elements accessed as array slice
Useful for variable-length tuples
Full Transcript
This example shows how to use rest elements in TypeScript tuples. The tuple type [number, ...string[]] means the first element is a number, followed by any number of strings collected into an array. When assigning [1, 'a', 'b', 'c'], the first element is 1, and the rest element collects ['a', 'b', 'c']. Accessing tuple[0] gives 1, and tuple.slice(1) gives the rest array. Rest elements must be last in the tuple type. This helps handle tuples with fixed and variable parts.

Practice

(1/5)
1.

What is the main purpose of using rest elements in TypeScript tuples?

easy
A. To define the exact fixed length of a tuple
B. To make tuples immutable
C. To allow tuples to contain only one type of element
D. To collect multiple elements into a single tuple part

Solution

  1. Step 1: Understand rest elements in tuples

    Rest elements gather multiple items into one part of a tuple, allowing flexible length.
  2. Step 2: Identify the purpose

    This means rest elements help collect many elements into one part of a tuple, not fix length or types.
  3. Final Answer:

    To collect multiple elements into a single tuple part -> Option D
  4. Quick Check:

    Rest elements collect multiple items [OK]
Hint: Rest elements gather extra items [OK]
Common Mistakes:
  • Thinking rest elements fix tuple length
  • Using ... without array type
  • Confusing rest elements with tuple immutability
2.

Which of the following is the correct syntax for a tuple with a rest element in TypeScript?

type MyTuple = [number, ...string[]];
easy
A. type MyTuple = [number, string...];
B. type MyTuple = [...number, string[]];
C. type MyTuple = [number, ...string[]];
D. type MyTuple = [number, ...string];

Solution

  1. Step 1: Check rest element syntax

    Rest elements use three dots (...) before a type array, placed at the end.
  2. Step 2: Validate each option

    type MyTuple = [number, ...string[]]; correctly uses [...string[]] after number. Others misuse syntax or placement.
  3. Final Answer:

    type MyTuple = [number, ...string[]]; -> Option C
  4. Quick Check:

    Rest element syntax = ...type[] at end [OK]
Hint: Rest element syntax: ...type[] at tuple end [OK]
Common Mistakes:
  • Placing rest element in the middle
  • Omitting brackets around rest type
  • Using ...type instead of ...type[]
3.

What will be the output of the following TypeScript code?

type Tuple = [string, ...number[]];
const example: Tuple = ['start', 1, 2, 3];
console.log(example.length);
medium
A. 1
B. 4
C. Error
D. 3

Solution

  1. Step 1: Understand tuple and rest element

    The tuple starts with a string, then any number of numbers via rest element.
  2. Step 2: Count elements in example

    example has 'start', 1, 2, 3 which is 4 elements total.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    Tuple length counts all elements including rest [OK]
Hint: Tuple length includes rest elements count [OK]
Common Mistakes:
  • Counting only fixed elements ignoring rest
  • Expecting error due to rest element
  • Confusing tuple length with rest array length
4.

Identify the error in this tuple type declaration:

type WrongTuple = [...string, number];
medium
A. Rest element must be an array type
B. Rest element must be at the end of the tuple
C. Tuple cannot have more than two elements
D. Number type cannot be used in tuples

Solution

  1. Step 1: Check rest element syntax

    Rest elements must use an array or tuple type after ..., like ...string[].
  2. Step 2: Analyze given tuple

    Here, ...string uses primitive string, not array/tuple type, invalid syntax.
  3. Final Answer:

    Rest element must be an array type -> Option A
  4. Quick Check:

    Rest element type = array/tuple only [OK]
Hint: Rest element requires array/tuple type [OK]
Common Mistakes:
  • Using ... with non-array type like ...string
  • Thinking rest element can't be array type
  • Confusing tuple length limits
5.

Given the tuple type type Data = [string, ...number[], boolean], which of the following assignments is valid?

A) const a: Data = ['hello', 1, 2, true];
B) const b: Data = ['hello', 42];
C) const c: Data = ['hello', 1, 2, 3, false];
D) const d: Data = ['hello', 1, false, 2];
hard
A. Only A and C are valid
B. Only B and D are valid
C. Only C is valid
D. Only D is valid

Solution

  1. Step 1: Understand tuple with rest element in middle

    Type [string, ...number[], boolean] means string first, then any numbers, then boolean last.
  2. Step 2: Check each assignment

    A) Ends with true (boolean), numbers in middle: ['hello',1,2,true] valid.
    B) Missing boolean after numbers, only string and number: invalid.
    C) Ends with false (boolean), numbers in middle: valid.
    D) Boolean before number (false before 2): invalid order.
  3. Final Answer:

    Only A and C are valid -> Option A
  4. Quick Check:

    Rest numbers must be between string and boolean [OK]
Hint: Rest numbers must be between first string and last boolean [OK]
Common Mistakes:
  • Placing boolean before rest numbers
  • Omitting rest numbers entirely
  • Assuming rest can be anywhere
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard