Spread in Arrays and Objects
Spread values while preserving accurate types.
Spreading Values
The spread operator ... copies elements of an array or properties of an object into a new one. TypeScript tracks the resulting type precisely, which is key for immutable updates.
Array Spread
Spread two arrays into a new one to concatenate them. The result's element type is the union of both source element types.
const a = [1, 2]
const b = [3, 4]
const all = [...a, ...b]
console.log(all) // [1, 2, 3, 4]All lessons in this course
- Rest Parameters in Functions
- Spread in Arrays and Objects
- Tuple Rest Elements
- Typing Variadic Functions