Linter · Rules
no-dupe-class-members
Disallow two class members with the same name
When a class declares two methods with the same name, the last one silently replaces the first. Calls run the second method, and edits to the first one have no effect.
A getter and a setter with the same name are a pair, so they're allowed. So are a static member and an instance member with the same name, and name next to the private #name.
This rule is on by default in JavaScript files. It's off in TypeScript files, where TypeScript reports the same mistake.
Configure
On by default as an error. To turn it off:
bunfig.toml
[lint.rules]
"no-dupe-class-members" = "off" # or "warn"
Examples
Reported:
class Cart {
total() {
return this.items.length;
}
total() {
return this.items.reduce((sum, item) => sum + item.price, 0);
}
}
Not reported:
class Cart {
count() {
return this.items.length;
}
total() {
return this.items.reduce((sum, item) => sum + item.price, 0);
}
}
class Cart {
#discount = 0;
get discount() {
return this.#discount;
}
set discount(value) {
this.#discount = value;
}
static discount() {
return new Cart();
}
}
A field and a method with the same name are reported too:
class Cart {
total = 0;
total() {
return this.items.length;
}
}
In TypeScript, overload signatures don't count:
class Cart {
add(id: string): void;
add(id: string, quantity: number): void;
add(id: string, quantity = 1) {
this.items.push({ id, quantity });
}
}