Deep CloneInstallation
Installation
How to install and import @visulima/deep-clone
Last updated:
Installation
Install the package using your preferred package manager:
npm install @visulima/deep-clonepnpm add @visulima/deep-cloneyarn add @visulima/deep-clonebun add @visulima/deep-cloneImport Methods
Main Function
Import the primary deepClone function:
import { deepClone } from "@visulima/deep-clone";
const cloned = deepClone({ a: 1, b: { c: 2 } });Default Import
Alternative default import:
import deepClone from "@visulima/deep-clone";
const cloned = deepClone({ a: 1, b: { c: 2 } });Utility Functions
Import utility functions from submodules:
import { copyOwnProperties, getCleanClone } from "@visulima/deep-clone/utils";
const copy = copyOwnProperties({ a: 1, b: 2 });
const clean = getCleanClone({ a: 1, b: 2 });Custom Handlers
Import handlers for customization:
import type { Options, State } from "@visulima/deep-clone";
import { copyArrayStrict, copyObjectStrict } from "@visulima/deep-clone/handler";CommonJS
For CommonJS environments:
const { deepClone } = require("@visulima/deep-clone");
const { copyOwnProperties } = require("@visulima/deep-clone/utils");Requirements
- Node.js: ≥22.13 ≤25.x
- TypeScript: ≥5.0 (optional, for type definitions)
- Browser: ES6+ support required (WeakMap for circular references)
Verify Installation
Test the installation works correctly:
import { deepClone } from "@visulima/deep-clone";
const original = {
name: "test",
nested: { value: 42 }
};
const cloned = deepClone(original);
cloned.nested.value = 100;
console.log(original.nested.value); // 42 (unchanged)
console.log(cloned.nested.value); // 100 (modified)If the original object remains unchanged, installation was successful!
TypeScript Support
The package includes comprehensive TypeScript type definitions. The generic type parameter preserves your object's types:
import { deepClone } from "@visulima/deep-clone";
interface User {
id: number;
name: string;
settings: {
theme: "light" | "dark";
};
}
const user: User = {
id: 1,
name: "Alice",
settings: { theme: "dark" }
};
// TypeScript knows the returned type is User
const cloned: User = deepClone(user);The DeepReadwrite utility type removes readonly modifiers, allowing you to modify the cloned object:
import { deepClone } from "@visulima/deep-clone";
interface ReadonlyUser {
readonly id: number;
readonly settings: {
readonly theme: string;
};
}
const user: ReadonlyUser = {
id: 1,
settings: { theme: "dark" }
};
// Cloned object has mutable properties
const cloned = deepClone(user);
cloned.id = 2; // OK! readonly is removed
cloned.settings.theme = "light"; // OK!