{"id":10727,"library":"deep-copy-ts","title":"Deep Copy TS","description":"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.","status":"active","version":"0.5.4","language":"javascript","source_language":"en","source_url":"https://github.com/erikvullings/deep-copy-ts","tags":["javascript","typescript"],"install":[{"cmd":"npm install deep-copy-ts","lang":"bash","label":"npm"},{"cmd":"yarn add deep-copy-ts","lang":"bash","label":"yarn"},{"cmd":"pnpm add deep-copy-ts","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses named exports. Prefer ESM imports for modern TypeScript/JavaScript projects.","wrong":"const deepCopy = require('deep-copy-ts');","symbol":"deepCopy","correct":"import { deepCopy } from 'deep-copy-ts';"},{"note":"For CommonJS environments, destructure the named export. A default import will not work.","wrong":"import deepCopy from 'deep-copy-ts';","symbol":"deepCopy (CommonJS)","correct":"const { deepCopy } = require('deep-copy-ts');"},{"note":"While `deepCopy` is a function, you might import types for advanced scenarios or extending its behavior, though direct type imports for the function itself are less common.","symbol":"Type definitions","correct":"import type { DeepCopy } from 'deep-copy-ts';"}],"quickstart":{"code":"import { deepCopy } from 'deep-copy-ts';\n\nconst originalObject = {\n  primitive: 123,\n  text: 'hello world',\n  nested: {\n    array: [1, 2, { sub: 'item' }],\n    date: new Date('2023-01-01T12:00:00Z'),\n    regex: /test/gi,\n  },\n  func: () => console.log('This is a function'),\n  nullValue: null,\n  undefinedValue: undefined,\n  typedArray: new Uint8Array([10, 20, 30]),\n  buffer: new ArrayBuffer(8),\n  dataView: new DataView(new ArrayBuffer(16))\n};\n\nconst copiedObject = deepCopy(originalObject);\n\nconsole.log('Original object:', originalObject);\nconsole.log('Copied object:', copiedObject);\n\n// Verify deep copy by modifying a nested property\ncopiedObject.nested.array[0] = 999;\ncopiedObject.nested.date.setFullYear(2050);\n\nconsole.log('\\nAfter modifying copiedObject:');\nconsole.log('Original array element:', originalObject.nested.array[0]); // Should be 1\nconsole.log('Copied array element:', copiedObject.nested.array[0]);     // Should be 999\nconsole.log('Original date year:', originalObject.nested.date.getFullYear()); // Should be 2023\nconsole.log('Copied date year:', copiedObject.nested.date.getFullYear());     // Should be 2050\n\n// Functions are copied by reference\nconsole.log('Are functions strictly equal?', originalObject.func === copiedObject.func); // true","lang":"typescript","description":"This quickstart demonstrates how to use `deepCopy` to duplicate an object containing various primitive and complex data types, including nested structures, dates, regexps, functions, and typed arrays. It then verifies that modifications to the copied object's nested properties do not affect the original object, illustrating the deep cloning behavior. It also shows that functions are copied by reference."},"warnings":[{"fix":"Ensure browser compatibility requirements are met, potentially by avoiding these specific TypedArray types or implementing environment-specific fallbacks.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Be aware of this behavior and manually handle custom logic for duplicating functions or Promises if deep cloning their internal state is required.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Test the library's behavior with objects containing circular references in your specific environment. If an error occurs, consider pre-processing your objects to break circular references or using a different library that explicitly guarantees circular reference handling.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Pin to exact versions for production environments or conduct thorough regression testing when updating to new minor versions.","message":"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.","severity":"breaking","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","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`.","error":"TypeError: (0 , deep_copy_ts__WEBPACK_IMPORTED_MODULE_0__.deepCopy) is not a function"},{"fix":"Inspect 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.","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`).","error":"Maximum call stack size exceeded"}],"ecosystem":"npm"}