Skip to content

Latest commit

 

History

History
59 lines (41 loc) · 1.19 KB

prefer-structured-clone.md

File metadata and controls

59 lines (41 loc) · 1.19 KB

Prefer using structuredClone to create a deep clone

💼 This rule is enabled in the ✅ recommended config.

💡 This rule is manually fixable by editor suggestions.

structuredClone is the modern way to create a deep clone of a value.

Fail

const clone = JSON.parse(JSON.stringify(foo));
const clone = _.cloneDeep(foo);

Pass

const clone = structuredClone(foo);

Options

Type: object

functions

Type: string[]

You can also check custom functions that creates a deep clone.

_.cloneDeep() and lodash.cloneDeep() are always checked.

Example:

{
	'unicorn/prefer-structured-clone': [
		'error',
		{
			functions: [
				'cloneDeep',
				'utils.clone'
			]
		}
	]
}
// eslint unicorn/prefer-structured-clone: ["error", {"functions": ["utils.clone"]}]
const clone = utils.clone(foo); // Fails