1.38.0deepClone
Creates a deep clone of a value while preserving object prototypes, property descriptors, special built-in types, and circular references.
deepClone(value):TParameters
valueTrequiredThe value to clone.
Returns
TReturns a deep clone of the input value.
Errors
Throws a TypeError if the value contains a WeakMap.
Throws a TypeError if the value contains a WeakSet.
Examples
Basic usage
Creates an independent deep copy of a nested object.
const original = {
name: "Alice",
profile: {
age: 25,
},
};
const copy = deepClone(original);
copy.profile.age = 30;
console.log(original.profile.age);
// 25
console.log(copy.profile.age);
// 30Nested arrays
Nested arrays are cloned recursively.
const original = [
[1, 2],
[3, 4],
];
const copy = deepClone(original);
copy[0][0] = 99;
console.log(original[0][0]);
// 1
console.log(copy[0][0]);
// 99Date
Date instances are cloned with the same timestamp.
const original = new Date("2026-01-01");
const copy = deepClone(original);
console.log(copy.getTime() === original.getTime());
// true
console.log(copy === original);
// falseRegExp
RegExp instances preserve their source, flags, and lastIndex.
const original = /hello/gi;
original.lastIndex = 3;
const copy = deepClone(original);
console.log(copy.source);
// hello
console.log(copy.flags);
// gi
console.log(copy.lastIndex);
// 3Map
Map keys and values are cloned recursively.
const original = new Map([
["user", { name: "Alice" }],
]);
const copy = deepClone(original);
copy.get("user")!.name = "Bob";
console.log(original.get("user")!.name);
// Alice
console.log(copy.get("user")!.name);
// BobSet
Set values are cloned recursively.
const original = new Set([
{ id: 1 },
]);
const copy = deepClone(original);
const clonedItem = [...copy][0];
clonedItem.id = 2;
console.log([...original][0].id);
// 1
console.log(clonedItem.id);
// 2Circular references
Circular references are preserved without causing infinite recursion.
const original: {
name: string;
self?: unknown;
} = {
name: "Node",
};
original.self = original;
const copy = deepClone(original);
console.log(copy !== original);
// true
console.log(copy.self === copy);
// trueShared references
Multiple references to the same source object remain references to the same cloned object.
const shared = {
value: 42,
};
const original = {
first: shared,
second: shared,
};
const copy = deepClone(original);
console.log(copy.first === copy.second);
// true
console.log(copy.first === shared);
// falsePrototype preservation
Objects are created using the prototype of the original object.
class User {
constructor(public name: string) {}
}
const original = new User("Alice");
const copy = deepClone(original);
console.log(copy instanceof User);
// true
console.log(copy === original);
// falseProperty descriptors
Own property descriptors are preserved while their values are cloned.
const original = {};
Object.defineProperty(original, "value", {
value: { count: 1 },
writable: false,
enumerable: false,
configurable: true,
});
const copy = deepClone(original);
const descriptor = Object.getOwnPropertyDescriptor(
copy,
"value",
);
console.log(descriptor?.writable);
// false
console.log(descriptor?.enumerable);
// falsePrimitive values
Primitive values are returned directly because they do not require cloning.
console.log(deepClone(42));
// 42
console.log(deepClone("hello"));
// hello
console.log(deepClone(true));
// true
console.log(deepClone(null));
// nullWeakMap
WeakMap instances cannot be cloned and cause a TypeError.
const value = new WeakMap();
deepClone(value);
// TypeError: Cannot clone WeakMapWeakSet
WeakSet instances cannot be cloned and cause a TypeError.
const value = new WeakSet();
deepClone(value);
// TypeError: Cannot clone WeakSet