oxc-project/oxc · warning · OxcDiagnostic

Duplicate enum value `{value}`

Error message

Duplicate enum value `{value}`

What it means

typescript/no-duplicate-enum-values reports enum members whose initializer value was already used by an earlier member in the same enum. TypeScript allows duplicate values (only names must be unique), but they are usually mistakes, and for string enums they make the reverse mapping ambiguous for tooling. The diagnostic carries two labels: where the value was first used and where it is re-used.

Source

Thrown at crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs:26

use rustc_hash::FxHashMap;

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

fn no_duplicate_enum_values_diagnostic(
    first_init_span: Span,
    second_member: &TSEnumMember,
    value: &str,
) -> OxcDiagnostic {
    let second_name = second_member.id.static_name();
    // Unwrap will never panic since violations are only reported for members
    // with initializers.
    let second_init_span = second_member.initializer.as_ref().map(GetSpan::span).unwrap();

    OxcDiagnostic::warn(format!("Duplicate enum value `{value}`"))
        .with_help(format!("Give {second_name} a unique value"))
        .with_labels([
            first_init_span.label(format!("{value} is first used as an initializer here")),
            second_init_span.label("and is re-used here"),
        ])
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow duplicate enum member values.
    ///
    /// ### Why is this bad?
    ///
    /// Although TypeScript supports duplicate enum member values, people

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Give the duplicate member a unique value: `enum E { A = 1, B = 2 }`
  2. If the collision is intentional aliasing, name it clearly and disable the rule for that line, or define the alias via `const alias = E.A` outside the enum
  3. For flag enums, verify each member uses a distinct bit: `1 << 0`, `1 << 1`, ...

Example fix

// before
enum Status {
  Active = 1,
  Paused = 1,
  Closed = 2,
}

// after
enum Status {
  Active = 1,
  Paused = 3,
  Closed = 2,
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{
  "rules": { "typescript/no-duplicate-enum-values": "warn" }
}

// unit test: fail when an enum gains duplicate values
import { Status } from './status';
it('has unique enum values', () => {
  const values = Object.values(Status);
  expect(new Set(values).size).toBe(values.length);
});

Prevention

When it happens

Trigger: `enum E { A = 1, B = 1 }`, `enum Flags { X = 1 << 0, Y = 1, Z = 1 }`, or computed initializers that evaluate to the same value; the rule tracks values via static evaluation of member initializers and reports the second member with both initializer spans labeled.

Common situations: Copy-pasting enum members and forgetting to bump the value; bitwise-flag enums where two members get the same bit; refactors converting string enums to numeric ones; accidental collisions after inserting a member with an explicit value.

Related errors


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