// Created on savesnippets.com · https://savesnippets.com/1k2IXXqYxfs4Jk export function deepClone(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