Selecting a version will take you to the chosen version of the ESLint docs.

no-dupe-class-members

Disallow duplicate class members

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

如果在类成员中存在同名的声明,最后一个声明会无声地覆盖其他声明。 这可能会导致意外的行为。

/*eslint-env es6*/

class Foo {
  bar() { console.log("hello"); }
  bar() { console.log("goodbye"); }
}

var foo = new Foo();
foo.bar(); // goodbye

规则细节

这条规则旨在标记类成员中的重复名称的使用。

示例

使用此规则的错误示例:

Open in Playground
/*eslint no-dupe-class-members: "error"*/

class Foo {
  bar() { }
  bar() { }
}

class Foo {
  bar() { }
  get bar() { }
}

class Foo {
  bar;
  bar;
}

class Foo {
  bar;
  bar() { }
}

class Foo {
  static bar() { }
  static bar() { }
}

使用此规则的正确示例:

Open in Playground
/*eslint no-dupe-class-members: "error"*/

class Foo {
  bar() { }
  qux() { }
}

class Foo {
  get bar() { }
  set bar(value) { }
}

class Foo {
  bar;
  qux;
}

class Foo {
  bar;
  qux() { }
}

class Foo {
  static bar() { }
  bar() { }
}

何时不用

不应该在 ES3/5 环境中使用此规则。

在 ES2015(ES6)或更高版本中,如果你不想被通知类成员中的重复名称,你可以安全地禁用这个规则。

Handled by TypeScript

It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.

Version

This rule was introduced in ESLint v1.2.0.

Resources

主题切换

切换到你所选择语言版本对应的 ESLint 网站。