ProgramGuru

Object Spread and Rest

Understand Object Spread and Rest

Object spread copies enumerable own properties into a new object. Object rest collects remaining enumerable own properties into a new object.

What you will learn: the purpose of object spread and rest, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how object spread and rest works and where its type rules apply.
  • Predict the exact output of the object spread and rest example.
  • Diagnose a common issue: Do not assume spread deeply clones nested values.

Build the mental model

Spread is shallow: nested objects are still shared references. When later spreads contain the same property key, the later value wins. This is useful for nonmutating top-level updates, but does not guarantee deep independence.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Object Spread and Rest: explained visuallyOriginal object → { ...original, score:90 } → New top-level objectOriginal object{ ...original, score:90}New top-level objectFollow the values through the operation
Object Spread and Rest: Original object → { ...original, score:90 } → New top-level object.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

const original = { name: "Priya", score: 72, city: "Jaipur" };
const updated = { ...original, score: 90 };
const { city, ...withoutCity } = updated;
console.log(original.score);
console.log(updated.score);
console.log(city);
console.log(Object.keys(withoutCity).sort().join(", "));

Verified output

72
90
Jaipur
name, score

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

Spread creates a new top-level object with score overwritten to 90. original remains at 72. Destructuring extracts city and rest collects name and score.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Do not assume spread deeply clones nested values. For nested immutable updates, copy each changed nesting level.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Add a nested contact field, spread-copy the object, then update contact.phone. Explain the shared reference and fix it with nested spread.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

What happens when a later spread or literal property repeats a key?
Choose one answer. You can try again.

Answer and explanation

The later value overwrites the earlier one.

Object spread copies enumerable own properties into a new object. Object rest collects remaining enumerable own properties into a new object.

Review the key idea

Object Spread and Rest: Object spread copies enumerable own properties into a new object.

Spread creates a new top-level object with score overwritten to 90. original remains at 72. Destructuring extracts city and rest collects name and score.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Introduction to Type Aliases →