Spread in Arrays and Objects
Spread values while preserving accurate types.
Spread in Arrays and Objects is a free TypeScript Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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]Mixing Spreads and Literals
You can interleave spread elements with individual values. Order is preserved exactly as written.
const middle = [2, 3]
const nums = [1, ...middle, 4]
console.log(nums) // [1, 2, 3, 4]Spread Element Types
When the spread sources have different element types, the result is an array of their union. Annotate or let inference do the work.
const ns: number[] = [1, 2]
const ss: string[] = ["a"]
const mixed = [...ns, ...ss] // (number | string)[]
console.log(mixed)Object Spread
Object spread copies own enumerable properties into a new object. The result type merges the source shapes.
const base = { id: 1, name: "Ada" }
const extended = { ...base, active: true }
console.log(extended) // { id: 1, name: 'Ada', active: true }Spread Overriding Properties
When keys collide, the last one wins. Spread an object, then override specific fields — the basis of immutable updates.
const user = { name: "Ada", age: 36 }
const older = { ...user, age: 37 }
console.log(older.age) // 37Order Determines the Winner
Property precedence follows source order. Put overrides after the spread to win; before, and the spread overwrites them.
const defaults = { theme: "light", size: "m" }
const result = { theme: "dark", ...defaults }
console.log(result.theme) // light — spread came lastType of an Overridden Property
If an override changes a property's type, the result type reflects the new type. TypeScript narrows to the latest occurrence.
const obj = { value: 1 as number }
const updated = { ...obj, value: "now a string" }
// updated.value is string
console.log(updated.value.toUpperCase())Spread Is a Shallow Copy
Spread copies one level deep. Nested objects are shared by reference, so mutating a nested field affects both copies. Spread the nested object too for a deeper copy.
const a = { nested: { x: 1 } }
const b = { ...a }
b.nested.x = 99
console.log(a.nested.x) // 99 — shared referenceSpread and readonly
Spreading a readonly array or object produces a fresh, mutable copy. The readonly modifier does not carry through a spread, since you are building a new value.
const frozen: readonly number[] = [1, 2, 3]
const copy = [...frozen] // number[], mutable
copy.push(4)
console.log(copy.length) // 4Immutable Update Pattern
Spread is the idiomatic way to update state without mutation: copy the old object, override the changed fields, and return the new object.
interface State { count: number; name: string }
function increment(s: State): State {
return { ...s, count: s.count + 1 }
}
console.log(increment({ count: 1, name: "x" }))Quick Check
Test your understanding of spread types and precedence.
Recap
Spread copies array elements or object properties into a new value. Array spread concatenates with a union element type; object spread merges shapes, and on key collisions the last occurrence wins. Spread is a shallow copy — nested objects stay shared — and it produces fresh, mutable values even from readonly sources. It is the idiomatic immutable-update pattern.
Frequently asked questions
Is the “Spread in Arrays and Objects” lesson free?
Yes — the full text of “Spread in Arrays and Objects” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Spread in Arrays and Objects”?
Spread values while preserving accurate types. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Spread in Arrays and Objects” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Rest Parameters in Functions
- Spread in Arrays and Objects
- Tuple Rest Elements
- Typing Variadic Functions