# 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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as an error. To turn it off:

```toml bunfig.toml
[lint.rules]
"no-dupe-class-members" = "off" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
class Cart {
  total() {
    return this.items.length;
  }

  total() {
    return this.items.reduce((sum, item) => sum + item.price, 0);
  }
}
```

Not reported:

{/* good */}
```js
class Cart {
  count() {
    return this.items.length;
  }

  total() {
    return this.items.reduce((sum, item) => sum + item.price, 0);
  }
}
```

{/* good */}
```js
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:

{/* bad */}
```js
class Cart {
  total = 0;

  total() {
    return this.items.length;
  }
}
```

In TypeScript, overload signatures don't count:

{/* good */}
```ts
class Cart {
  add(id: string): void;
  add(id: string, quantity: number): void;
  add(id: string, quantity = 1) {
    this.items.push({ id, quantity });
  }
}
```
