Skip to content
← Back to rules

unicorn/no-useless-fallback-in-spread Correctness

This rule is turned on by default.
🛠️ An auto-fix is available for this rule for some violations.

它的作用

禁止在对象字面量中展开时使用无用的回退值。

为什么这不好?

在对象字面量中展开 falsy 值 不会添加任何意外的属性,因此没有必要添加一个空对象作为回退。

示例

以下是此规则的错误代码示例:

javascript
const object = { ...(foo || {}) };

以下是此规则的正确代码示例:

javascript
const object = { ...foo };
const object = { ...(foo || { not: "empty" }) };

如何使用

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

json
{
  "rules": {
    "unicorn/no-useless-fallback-in-spread": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-useless-fallback-in-spread": "error",
  },
});
bash
oxlint --deny unicorn/no-useless-fallback-in-spread

版本

此规则于 v0.0.16 中添加。

参考资料