Skip to content

noDuplicateClassMembers

诊断类别:lint/suspicious/noDuplicateClassMembers

¥Diagnostic Category: lint/suspicious/noDuplicateClassMembers

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止重复的类成员。

¥Disallow duplicate class members.

如果类成员中有同名声明,则最后一个声明将默默覆盖其他声明。它可能导致意外行为。

¥If there are declarations of the same name among class members, the last declaration overwrites other declarations silently. It can cause unexpected behaviours.

¥Examples

¥Invalid

class Foo {
bar() { }
bar() { }
}
code-block.js:3:3 lint/suspicious/noDuplicateClassMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Duplicate class member name “bar”

1 │ class Foo {
2 │ bar() { }
> 3 │ bar() { }
^^^^^^^^^
4 │ }
5 │

class Foo {
bar() { }
get bar() { }
}
code-block.js:3:3 lint/suspicious/noDuplicateClassMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Duplicate class member name “bar”

1 │ class Foo {
2 │ bar() { }
> 3 │ get bar() { }
^^^^^^^^^^^^^
4 │ }
5 │

class Foo {
bar;
bar() { }
}
code-block.js:3:3 lint/suspicious/noDuplicateClassMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Duplicate class member name “bar”

1 │ class Foo {
2 │ bar;
> 3 │ bar() { }
^^^^^^^^^
4 │ }
5 │

class Foo {
static bar() { }
static bar() { }
}
code-block.js:3:3 lint/suspicious/noDuplicateClassMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Duplicate class member name “bar”

1 │ class Foo {
2 │ static bar() { }
> 3 │ static bar() { }
^^^^^^^^^^^^^^^^
4 │ }
5 │

¥Valid

class Foo {
bar() { }
qux() { }
}
class Foo {
set bar(value) { }
get bar() { }
}
class Foo {
bar;
qux;
}
class Foo {
bar;
qux() { }
}
class Foo {
static bar() { }
bar() { }
}

¥Related links