Skip to content
← Back to rules

oxc/bad-object-literal-comparison 正确性

This rule is turned on by default.

它的作用

检查对象字面量和数组字面量之间的比较。

为什么这不好?

将变量与对象或数组字面量进行比较,结果总是 false,因为对象和数组字面量彼此之间永远不相等。

如果你想检查对象或数组是否为空,请使用 Object.entries()Object.keys() 及其长度。

示例

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

javascript
if (x === {}) {
}
if (arr !== []) {
}

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

javascript
if (typeof x === "object" && Object.keys(x).length === 0) {
}
if (Array.isArray(x) && x.length === 0) {
}

如何使用

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

json
{
  "rules": {
    "oxc/bad-object-literal-comparison": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "oxc/bad-object-literal-comparison": "error",
  },
});
bash
oxlint --deny oxc/bad-object-literal-comparison

版本

此规则是在 v0.1.1 中添加的。

参考资料