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