Skip to content
← Back to rules

eslint/no-dupe-class-members Correctness

This rule is turned on by default.
An auto-fix is available for this rule.

作用

禁止重复的类成员。

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

为什么不好?

如果类成员中存在相同名称的声明,最后的声明会静默覆盖其他声明。这可能导致意外行为。

示例

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

javascript
class A {
  foo() {
    console.log("foo");
  }
  foo = 123;
}
let a = new A();
a.foo(); // 未捕获的 TypeError: a.foo 不是函数

此规则正确代码的示例:

javascript
class A {
  foo() {
    console.log("foo");
  }
}
let a = new A();
a.foo();

如何使用

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

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

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

版本

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

参考资料