Skip to content
← Back to rules

eslint/no-dupe-keys Correctness

This rule is turned on by default.

作用

禁止在对象字面量中出现重复的键。

对于 TypeScript 代码,可以禁用此规则,因为 TypeScript 编译器会强制进行此检查。

为什么不好?

对象字面量中具有相同键的多个属性会导致应用程序中出现意外行为。

示例

此规则不正确代码的示例:

js
var foo = {
  bar: "baz",
  bar: "qux",
};

var foo = {
  bar: "baz",
  bar: "qux",
};

var foo = {
  0x1: "baz",
  1: "qux",
};

此规则正确代码的示例:

js
var foo = {
  bar: "baz",
  qux: "qux",
};

如何使用

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

json
{
  "rules": {
    "no-dupe-keys": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-dupe-keys": "error",
  },
});
bash
oxlint --deny no-dupe-keys

版本

此规则于 v0.0.3 中加入。

参考资料