title | rule_type | handled_by_typescript |
---|---|---|
no-dupe-class-members |
problem |
true |
If there are declarations of the same name in class members, the last declaration overwrites other declarations silently. It can cause unexpected behaviors.
/*eslint-env es6*/
class Foo {
bar() { console.log("hello"); }
bar() { console.log("goodbye"); }
}
var foo = new Foo();
foo.bar(); // goodbye
This rule is aimed to flag the use of duplicate names in class members.
Examples of incorrect code for this rule:
::: incorrect
/*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() { }
}
:::
Examples of correct code for this rule:
::: correct
/*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() { }
}
:::
This rule should not be used in ES3/5 environments.
In ES2015 (ES6) or later, if you don't want to be notified about duplicate names in class members, you can safely disable this rule.