Skip to content
← Back to rules

unicorn/consistent-empty-array-spread Pedantic

💡 A suggestion is available for this rule.

作用

在数组中展开三元表达式时,我们既可以使用 [] 也可以使用 '' 作为回退值, 但最好让两个分支中的类型保持一致。

这有什么问题?

让两个分支中的类型保持一致会使代码更易于阅读和理解。

示例

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

javascript
const array = [a, ...(foo ? [b, c] : "")];

const array = [a, ...(foo ? "bc" : [])];

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

javascript
const array = [a, ...(foo ? [b, c] : [])];

const array = [a, ...(foo ? "bc" : "")];

如何使用

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

json
{
  "rules": {
    "unicorn/consistent-empty-array-spread": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/consistent-empty-array-spread": "error",
  },
});
bash
oxlint --deny unicorn/consistent-empty-array-spread

版本

此规则于 v0.10.1 中添加。

参考资料