An object literal gives the same key twice

tx0/ts:duplicate-keydefaultwarningbug

What it finds

{ a: 1, a: 2 }: when an object literal gives the same key twice, the later value replaces the earlier one, which is then written for nothing.

Keys are compared as JavaScript compares them: names and strings by their text, so a, 'a' and "\x61" are the same key, and numbers by the string they convert to, so 1, 1.0 and '1' are too. A getter and a setter of the same name make one property and are not a repetition. Computed keys are not compared, and a spread between two equal keys starts over, since what it copies may already replace the first. __proto__ sets the prototype rather than a property and is left alone. An object literal that is the target of a destructuring assignment reads properties rather than giving them, and may read one twice.

From the rule's source, duplicate_key.rs.

Example from the rule's tests
const settings = {  name: "a",  size: 1,  "name": "b", // want "the key `"name"` is given again"  1: "one",  1.0: "again",tx0 reports: the key 1.0 is given again  get value() { return 1; },  set value(v) {},  [computed]: 1,  [computed]: 2,}; const merged = { size: 1, ...defaults, size: 2 };const proto = { __proto__: null, __proto__: Object.prototype };const shorthand = { size: 1, size };tx0 reports: the key size is given again let first, second;({ name: first, name: second } = settings); declare const computed: string, defaults: object, size: number;
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:duplicate-key" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:duplicate-key] reason="..."

tx0 explain tx0/ts:duplicate-key shows the same in the terminal. Found a false positive? Report it.