Skip to content
← Back to rules

unicorn/prefer-structured-clone Style

💡 A suggestion 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[]

default: ["cloneDeep", "utils.clone"]

允许用于深拷贝、而不是 structuredClone 的函数列表。

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-structured-clone": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-structured-clone": "error",
  },
});
bash
oxlint --deny unicorn/prefer-structured-clone

版本

此规则于 v0.9.0 中添加。

参考