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-clone
pnpm add @visulima/deep-clone
yarn add @visulima/deep-clone
bun add @visulima/deep-clone

Import 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!

Next Steps

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues