Deep Copy TS
deep-copy-ts is a utility library for JavaScript and TypeScript designed to create robust deep copies (clones) of various data structures. It accurately duplicates a broad spectrum of primitive values, objects, arrays, and complex built-in types such as Date, RegExp, ArrayBuffer, DataView, and various TypedArrays (Float32Array, Int8Array, etc.). The current stable version is 0.5.4, indicating it is pre-1.0.0 and may undergo breaking changes in future minor releases. While a specific release cadence is not published, the active versioning suggests ongoing development. A key differentiator is its comprehensive handling of numerous JavaScript built-in types and its TypeScript-first approach, offering type safety and reliable deep cloning without mutating the original object.
Common errors
-
TypeError: (0 , deep_copy_ts__WEBPACK_IMPORTED_MODULE_0__.deepCopy) is not a function
cause This error often occurs in bundler environments (e.g., Webpack, Rollup) when there's a mismatch between CommonJS `require` and ES Module `import` syntax, or if the bundler struggles to resolve the named export `deepCopy`.fixEnsure you are using `import { deepCopy } from 'deep-copy-ts';` for ES Modules. If in a CommonJS-only environment, use `const { deepCopy } = require('deep-copy-ts');`. Verify your bundler configuration correctly handles ES Modules. -
Maximum call stack size exceeded
cause This error typically indicates an infinite recursion. While not explicitly stated in the documentation, it can occur in deep copying libraries when processing objects with circular references (e.g., `obj.a = obj`).fixInspect the object you are attempting to copy for circular references. If they exist, either restructure your data to avoid them or use a deep cloning utility that explicitly supports and handles circular references, often by tracking visited objects.
Warnings
- gotcha The library's support for `BigInt64Array` and `BigUint64Array` is noted as 'not supported in Safari yet' in the README. Use caution or polyfills if targeting Safari and these types are critical.
- gotcha Functions and Promises are copied by reference, not deeply cloned. If a function contains internal state or references that need to be duplicated, deep-copy-ts will not handle this, and the copied object will share the same function instance as the original.
- gotcha While the library handles many common types, objects with circular references are not explicitly documented. Attempting to deep copy an object with a direct or indirect circular reference might lead to an infinite loop and a 'Maximum call stack size exceeded' error if not properly handled by the library internally.
- breaking As a pre-1.0.0 package (current version 0.5.4), deep-copy-ts may introduce breaking changes in minor version updates without adhering to strict semver major-version increments. Review release notes carefully when upgrading minor versions.
Install
-
npm install deep-copy-ts -
yarn add deep-copy-ts -
pnpm add deep-copy-ts
Imports
- deepCopy
const deepCopy = require('deep-copy-ts');import { deepCopy } from 'deep-copy-ts'; - deepCopy (CommonJS)
import deepCopy from 'deep-copy-ts';
const { deepCopy } = require('deep-copy-ts'); - Type definitions
import type { DeepCopy } from 'deep-copy-ts';
Quickstart
import { deepCopy } from 'deep-copy-ts';
const originalObject = {
primitive: 123,
text: 'hello world',
nested: {
array: [1, 2, { sub: 'item' }],
date: new Date('2023-01-01T12:00:00Z'),
regex: /test/gi,
},
func: () => console.log('This is a function'),
nullValue: null,
undefinedValue: undefined,
typedArray: new Uint8Array([10, 20, 30]),
buffer: new ArrayBuffer(8),
dataView: new DataView(new ArrayBuffer(16))
};
const copiedObject = deepCopy(originalObject);
console.log('Original object:', originalObject);
console.log('Copied object:', copiedObject);
// Verify deep copy by modifying a nested property
copiedObject.nested.array[0] = 999;
copiedObject.nested.date.setFullYear(2050);
console.log('\nAfter modifying copiedObject:');
console.log('Original array element:', originalObject.nested.array[0]); // Should be 1
console.log('Copied array element:', copiedObject.nested.array[0]); // Should be 999
console.log('Original date year:', originalObject.nested.date.getFullYear()); // Should be 2023
console.log('Copied date year:', copiedObject.nested.date.getFullYear()); // Should be 2050
// Functions are copied by reference
console.log('Are functions strictly equal?', originalObject.func === copiedObject.func); // true