Avoid duplicate enum member values

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: typescript-best-practices/no-duplicate-enum-values

Language: TypeScript

Severity: Warning

Category: Error Prone

Description

An enum should not have duplicate values, which are usually not expected to be present.

Non-Compliant Code Examples

enum A {
    A = 1,
    B = 1,
}
enum B {
    A = 'A',
    B = 'A',
}
enum C {
  A = 'A',
  B = 'A',
  C = 1,
  D = 1,
}
enum E {
    A = 'A',
    B = 'A',
    C = 1,
    D = 1,
}

Compliant Code Examples

enum A {
  A,
  B,
}

enum B {
  A = 1,
  B,
}

enum C {
  A = 1,
  B = 2,
}

enum D {
  A = 'A',
  B = 'B',
}

enum E {
  A = 'A',
  B = 'B',
  C,
}

enum F {
  A = 'A',
  B = 'B',
  C = 2,
  D = 1 + 1,
}

enum G {
  A = 3,
  B = 2,
  C,
}

enum H {
  A = 'A',
  B = 'B',
  C = 2,
  D = foo(),
}

enum I {
  A = '',
  B = 0,
}

enum J {
  A = 0,
  B = -0,
  C = NaN,
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis