oxc-project/oxc · warning

Duplicate key '{key}'

Error message

Duplicate key '{key}'

What it means

This diagnostic comes from the `no_dupe_keys` rule in oxlint. It reports an object literal that defines the same key twice. The later value overwrites the earlier one at runtime, so the first pair is dead data. The rule stores each `PropertyKey` in a hash map and reports the second occurrence with labels on both spans.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_dupe_keys.rs:13

use oxc_ast::{
    AstKind,
    ast::{ObjectProperty, ObjectPropertyKind, PropertyKey, PropertyKind},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use rustc_hash::{FxBuildHasher, FxHashMap};

use crate::{AstNode, context::LintContext, rule::Rule};

fn no_dupe_keys_diagnostic(first: Span, second: Span, key: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Duplicate key '{key}'"))
        .with_help("Consider removing the duplicated key")
        .with_labels([
            first.label("Key is first defined here"),
            second.label("and duplicated here"),
        ])
}

#[derive(Debug, Default, Clone)]
pub struct NoDupeKeys;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow duplicate keys in object literals.
    ///
    /// This rule can be disabled for TypeScript code, as the TypeScript compiler
    /// enforces this check.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete one of the two pairs; the report labels the first definition and the duplicate.
  2. Rename one key when both values matter.
  3. Add a check to code review for large literal config objects.

Example fix

// before
const config = {
  retries: 2,
  timeout: 500,
  retries: 5,
};

// after
const config = {
  retries: 5,
  timeout: 500,
};
Defensive patterns

Strategy: validation

Validate before calling

// no reliable runtime check: the engine collapses duplicate keys silently
// run oxlint on the file instead:
//   oxlint src/config.js

Prevention

When it happens

Trigger: An object literal with a repeated key: `{ a: 1, a: 2 }`, a quoted and unquoted mix `{ 'x': 1, x: 2 }`, or numeric forms that hold the same value such as `{ 1: 'a', 0x1: 'b' }`. Spread properties do not count.

Common situations: A config object grows over many commits, and two people add the same key. Keys are renamed during a migration and the old key stays in one place. Option groups are copied and pasted.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/e5924b80cb26fe7a. Report an issue: GitHub.