export function deepClone<T>(value: T): T {
if (typeof structuredClone === 'function') {
return structuredClone(value);
}
// Older-runtime fallback (loses Dates/Maps/Sets fidelity)
return JSON.parse(JSON.stringify(value)) as T;
}
const original = {
date: new Date(),
map: new Map([['a', 1]]),
nested: { items: [1, 2, 3] },
};
const copy = deepClone(original);
copy.nested.items.push(4);
console.log(original.nested.items); // [1, 2, 3] — untouched
console.log(copy.date instanceof Date); // true — Date preserved
Create a free account and build your private vault. Share publicly whenever you want.