cloneDeep()
cloneDeep<
T>(value):T
Creates a deep clone of a value using the native structuredClone API.
DEPRECATED
Use the native structuredClone() function directly instead.
Type Parametersβ
T: Tβ
The type of the value to clone.
Parametersβ
value: Tβ
The value to clone deeply.
Returns: Tβ
A deep clone of the input value.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
clone (Remeda, Ramda) Β· cloneDeep (Lodash, es-toolkit, Radashi) Β· β (Effect, Modern Dash, Antfu)
Exampleβ
const original = { a: 1, b: { c: 2 } };
// β Deprecated approach
const cloned = cloneDeep(original);
cloned.b.c = 3;
console.log(original.b.c); // 2 (unchanged)
// β
Recommended approach
const clonedNative = structuredClone(original);
clonedNative.b.c = 3;
console.log(original.b.c); // 2 (unchanged)
How it works?β
Creates a deep clone of a value.
Deprecated: Use structuredClone() directly (ES2022) or Arkhe's deepClone.
Native Equivalentβ
// β cloneDeep(obj)
// β
structuredClone(obj)
// β
deepClone(obj) // Arkhe
Use Casesβ
Deep copy object πβ
Create deep copy of object.
structuredClone(obj);
// or
JSON.parse(JSON.stringify(obj));
Immutable updatesβ
Clone before modifying.
const newState = structuredClone(state);
newState.user.name = "New Name";
Isolate dataβ
Create independent copy.
const copy = structuredClone(original);
// Modifications don't affect original