← Fundamentals
Fundamentals

Shallow and deep copying

Which objects are new, and which references should remain live?

Choose which objects a copy replaces and which sharing relationships it preserves.

Outcome

Trace copy depth and object identity, then implement a selective fork preserving the intended protected and live sharing relationships.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Shallow object copy
A new outer object that retains its existing nested references.
Copy boundary
The objects that a copy policy duplicates, with references beyond that boundary intentionally retained.
Alias group
Access paths identifying the same object, rather than merely equal object values.
Selective fork
A new structure that detaches a chosen shared object group while preserving specified live links elsewhere.

Copy depth and shared identity

A nested data structure connects objects through references or pointer-like values. A shallow copy duplicates an outer value while retaining its nested access paths. A deeper copy creates independent objects at additional levels; the operation and the field types determine where that boundary lies.

A copy policy must also account for intentional sharing. Two paths may reach the same object, while equal-looking objects may have separate identities. Decide which links remain live and which become local before copying a graph; copying more objects is not automatically the desired result.

01The copied level

A new outer record can still share a nested cell

A graphics document’s rows link to mutable palette swatches. Duplicate row keeps its library swatch linked; detach row gives one row its own swatch. Creating a document variant can instead make one selected swatch group local, keeping that group shared inside the variant while other library links stay live.

The graphics editor duplicates a row while keeping its library swatch linked. This reduced probe copies a row, edits its own code and then edits the swatch through it. Predict which original fields are affected.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Cell = { value: number };
type Row = { code: number; cell: Cell };

const original: Row = { code: 3, cell: { value: 5 } };
const copy = { ...original };

copy.code = 9;
copy.cell.value = 7;

console.log("original", original.code, original.cell.value);
console.log("copy", copy.code, copy.cell.value);
Which report prints?
Loading A new Row can still reach the same Cell…
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.

02Copying another level

An independent cell requires another ownership boundary

Detach row needs a swatch that can be edited without changing the library. First isolate that allocation boundary: this probe copies one standalone Cell with its current value. Predict the original and copy after editing the copy.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Cell = { value: number };

const original: Cell = { value: 5 };
const copy = structuredClone(original);

copy.value = 7;

console.log("original", original.value, "copy", copy.value);
Which report follows the standalone swatch copy?

03Identity and internal sharing

Equal values can belong to different alias groups

Several document rows can intentionally share one library swatch. Here two rows link to shared and a third links to a separate, equal-valued swatch. Predict the three values after editing through the first row; the variant tool must preserve this identity distinction.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Cell = { value: number };
type Row = { code: number; cell: Cell };

const shared: Cell = { value: 5 };
const equal: Cell = { value: 5 };
const rows: Row[] = [
  { code: 1, cell: shared },
  { code: 2, cell: shared },
  { code: 3, cell: equal }
];

rows[0].cell.value = 8;

console.log("cells", ...rows.map((row) => row.cell.value));
Which report prints?

04Practice

Repair the palette document

Repair the palette-link editor: duplicate a row while retaining its linked swatch, detach one row and swatch for an independent edit, then create a document variant with one chosen swatch group made local.

The numeric cell value is the editable swatch setting. Shared cells model intentional palette links. Unselected swatches remain linked to their original cells, and equal values alone never establish a link.

Start the lab

Copy the requested level, preserve a nested value independently, then implement the selective document boundary.

  1. easy · F-COPY-V4-1 Copy one object level
  2. medium · F-COPY-V4-2 Separate a nested value
  3. hard · F-COPY-V4-3 Preserve a selective boundary
In the frontend Copy depth describes the data boundary; state publication is a separate framework step. Optional

A swatch preview can keep an earlier plain-data proof while the working numeric setting changes. This excerpt isolates one Cell; surrounding row and document sharing policies still need their own boundaries.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
'use client';

import { useState } from 'react';

export function SwatchEditor() {
	const [cell, setCell] = useState({ value: 5 });

	function paint() {
		setCell((previous) => ({ value: previous.value + 1 }));
	}

	return <button onClick={paint}>{cell.value}</button>;
}

The updater returns a new Cell object rather than mutating the previous state object. The current value is carried into that new object before the increment.

Sources: React: Updating objects in state,Svelte: state snapshots.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.