Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Use structuredClone() instead of workarounds like JSON.parse(JSON.stringify(…)) or lodash’s cloneDeep. structuredClone is the modern, built-in way to create deep clones and correctly handles types that JSON cannot round-trip (such as Date, Map, Set, ArrayBuffer, and circular references).
Recommendedrecommended · ☑️ unopinionated
Suggestions💡 Has suggestions
This rule only provides suggestions (not auto-fixes), because the semantics of structuredClone differ subtly from JSON.parse(JSON.stringify(…)) — for example, structuredClone preserves undefined and throws on non-cloneable values.

Examples

// JSON round-trip hack
const clone = JSON.parse(JSON.stringify(foo));

// Lodash deep clone (always checked)
const clone = _.cloneDeep(foo);
const clone = lodash.cloneDeep(foo);

Options

Type: object

functions

Type: string[]
Default: []
Additional function names or paths to flag as deep-clone helpers. _.cloneDeep() and lodash.cloneDeep() are always checked.
{
  'unicorn/prefer-structured-clone': [
    'error',
    {
      functions: [
        'cloneDeep',
        'utils.clone'
      ]
    }
  ]
}
/* eslint unicorn/prefer-structured-clone: ["error", {"functions": ["utils.clone"]}] */

// Fails
const clone = utils.clone(foo);

Build docs developers (and LLMs) love