Same Key in Destructuring or Object Literal

ID

javascript.destructuring_same_key

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

dead-code, reliability

Description

Reports two cases where the same property name appears twice inside a single brace-delimited construct:

  1. Object destructuring patterns:

    const { a, a } = obj;          // FLAW
    const { id, id: copy } = row;  // FLAW
  2. Object literals:

    const cfg = { foo: 1, foo: 2 }; // FLAW

Rationale

In a destructuring pattern, only one of the bindings ever takes effect; the others are immediately overwritten or never reached. The duplicate is dead code and almost always a copy/paste mistake.

In a plain object literal, the last assignment wins. That is rarely what the author meant when they typed the second key with the same name — usually they intended a different key (typo) or are accidentally clobbering the first definition.

In strict mode, a duplicate key in an object literal used to be a SyntaxError (ES5); in modern engines the program runs but you still lose half the data you wrote.

Remediation

Pick one binding (or one definition) and remove the rest, or rename the duplicates if both were really intended:

// Good
const { a } = obj;
const { id, id: copy } = row;     // remove one branch
const cfg = { primary: 1, fallback: 2 };