no-dupe-keys

Disallow two keys with the same name in an object literal

error by default

When an object literal has the same key twice, the last value silently replaces the first. In a long object it's easy to add a key that's already there, and then one of the two has no effect.

A getter and a setter with the same name are a pair, so they're allowed.

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-keys" = "off" # or "warn"

Examples#

example.js1 problem
const options = {
  timeout: 1000,
  retries: 3,
  timeout: 5000,
};
example.jsNo problems
const options = {
  timeout: 5000,
  retries: 3,
};

Keys are compared by value, so "timeout" is the same key as timeout, and 0x1 is the same key as 1:

example.js2 problems
const options = {
  timeout: 1000,
  "timeout": 5000,
};

const labels = {
  1: "one",
  0x1: "uno",
};

A computed key such as [name] and a spread aren't compared, because their keys are only known at runtime:

example.jsNo problems
const options = {
  timeout: 1000,
  [name]: 5000,
  ...overrides,
};