DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Deep Clone an Object in TypeScript

Use structuredClone() for deep copies of supported JavaScript values in TypeScript. Learn what it preserves, where it falls short, and how to check runtime support.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For ordinary data supported by JavaScript’s structured clone algorithm, use structuredClone(value):

const copy = structuredClone(value);

It makes a deep copy of supported values, including nested objects, arrays and circular references. It does not preserve arbitrary class behavior, and the JavaScript runtime—not TypeScript alone—must provide the API. MDN documents structuredClone().

As an Amazon Associate I earn from qualifying purchases.

Clone nested objects with structuredClone()

A deep clone gives nested data independent references, so changing a nested object in the copy does not change the corresponding object in the original:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const original = {
  user: { name: "Ada" },
  tags: ["typescript", "javascript"],
};

const copy = structuredClone(original);
copy.user.name = "Grace";

// original.user.name remains "Ada"

This is suitable when the value consists of types supported by the structured clone algorithm. It is the simplest general-purpose choice for such data.

What structuredClone() can and cannot clone

Supported values include plain objects, arrays, dates, maps, sets, array buffers, data views, typed arrays, regular expressions, errors and primitive values other than symbols. Support for a type does not mean every aspect of its behavior or metadata is preserved. See MDN’s structured clone algorithm reference and its overview of serializable objects.

  • Functions and DOM nodes: these cannot be cloned; attempting to clone unsupported values can throw a DataCloneError.
  • Class instances: the prototype chain is not cloned, so the result does not retain the original class’s methods or identity. Private class elements are not preserved.
  • Property details: property descriptors and accessor behavior, including getters and setters, are not preserved.

If your code depends on a class’s invariants or methods, rebuild the value through its constructor or factory, or implement a clone method that captures the intended domain-specific behavior. A generic deep-clone API cannot infer those rules.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Circular references are supported

A self-reference does not make structuredClone() recurse forever. The algorithm tracks references it has already visited, allowing the copied graph to reproduce cycles:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const original: { label: string; self?: unknown } = { label: "node" };
original.self = original;

const copy = structuredClone(original);
// copy.self points back to copy.

Choose a cloning approach for your data

Approach Use it when Tradeoff
structuredClone(value) Your value is structured-cloneable and the runtime provides the API. Supports cycles and many built-in types, but does not preserve functions, DOM nodes, custom prototype behavior, descriptors or private fields. MDN
JSON.parse(JSON.stringify(value)) Your data is deliberately limited to JSON-serializable values. JSON serialization does not represent every JavaScript value or object property, so information can be omitted. MDN’s deep-copy overview
Explicit reconstruction or a type-specific clone method The copy must retain class identity, invariants or custom semantics. You must define which state and behavior the new instance should preserve.

The JSON round trip is a constrained option, not a universal deep-clone technique:

const copy = JSON.parse(JSON.stringify(value));

Check both TypeScript declarations and runtime support

If TypeScript reports that structuredClone is unknown, check the built-in library declarations selected by your project’s target and lib settings. TypeScript explains how its declarations are selected in the Type Declarations handbook and the Library Structures reference.

Then verify that the JavaScript runtime where the code will execute supports structuredClone(). A TypeScript declaration only tells the compiler about an API; it does not install or polyfill that API. Check the compatibility information for your specific browser, server runtime or deployment target rather than assuming one minimum version applies everywhere.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Copying is not the same as transferring

structuredClone() also accepts a transfer option for transferable resources. Transferring moves the resource to the clone; the original resource becomes unusable. Use that option only when moving ownership is intended, not when you need to keep using both the original and the copy. MDN’s method reference describes the option.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.