Implement a deepClone
utility that returns a deep-copied version of any plain JavaScript data (objects, arrays, and primitives).
Signature
function deepClone<T>(value: T): T
Requirements
- Supported inputs:
- Primitives (
string
, number
, boolean
, null
, undefined
, bigint
, symbol
)
- Plain objects (created with
{}
or Object.create(null)
)
- Arrays
- For objects and arrays, every nested level must be cloned by value, never by reference.
- Any value that is not a primitive, plain object, or array (e.g.
Date
, RegExp
, Map
, Set
, functions, DOM nodes) should be returned as-is (shared reference).
- Circular references are out of scope (you may assume the input is acyclic).
Example
const original = {
user: "Alice",
scores: [1, 2, 3],
};
const copy = deepClone(original);
copy.scores.push(4);
console.log(original.scores); // [1, 2, 3]
console.log(copy.scores); // [1, 2, 3, 4]