GuobaGuoba Utils

deepClone

@guoba-ai/utils / object / deepClone

Function: deepClone()

function deepClone<T>(obj): T;

Defined in: object.ts:60

Deep clone a value using structuredClone.

Parameters

obj

T

The value to clone

Returns

T

A deep copy of the value

Example

const original = { a: { b: 1 } }
const copy = deepClone(original)
copy.a.b = 2
original.a.b // still 1

deepClone([1, [2, [3]]]) // [1, [2, [3]]]

deepClone(new Date('2024-01-01')).getFullYear() // 2024

Warning

Values unsupported by structuredClone, such as functions, will throw.

On this page