Skip to content
← Back to rules

unicorn/prefer-structured-clone 风格

An auto-fix is available for this rule.

它做了什么

推荐使用 structuredClone 来创建深层克隆。

为什么这是个问题

structuredClone 是创建值的深层克隆的现代方式。

示例

此规则的错误代码示例:

js
const clone = JSON.parse(JSON.stringify(foo));

const clone = _.cloneDeep(foo);

此规则的正确代码示例:

js
const clone = structuredClone(foo);

配置

此规则接受一个配置对象,包含以下属性:

functions

type: string[]

默认值: ["cloneDeep", "utils.clone"]

允许用于深层克隆的函数列表,这些函数可替代 structuredClone 使用。

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/prefer-structured-clone": "error"
  }
}
bash
oxlint --deny unicorn/prefer-structured-clone

参考资料