Skip to content

Latest commit

Β 

History

History
59 lines (41 loc) Β· 1.24 KB

File metadata and controls

59 lines (41 loc) Β· 1.24 KB

prefer-structured-clone

πŸ“ Prefer using structuredClone to create a deep clone.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ’‘ This rule is manually fixable by editor suggestions.

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

Examples

// ❌
const clone = JSON.parse(JSON.stringify(foo));

// ❌
const clone = _.cloneDeep(foo);

// βœ…
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);