0
0
Typescriptprogramming~10 mins

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

Choose your learning style9 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.