Skip to content
← Back to rules

typescript/no-duplicate-enum-values Correctness

This rule is turned on by default.

作用

禁止枚举成员值重复。

为什么这不好?

尽管 TypeScript 支持重复的枚举成员值,但人们通常期望同一个枚举中的成员具有唯一值。重复的值可能会导致难以追踪的错误。

示例

此规则不允许定义一个有多个成员初始化为相同值的枚举。没有初始化器的成员不会被检查。

错误 示例代码:

ts
enum E {
  A = 0,
  B = 0,
}
ts
enum E {
  A = "A",
  B = "A",
}

正确 示例代码:

ts
enum E {
  A = 0,
  B = 1,
}
ts
enum E {
  A = "A",
  B = "B",
}
ts
enum E {
  A,
  B,
}

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/no-duplicate-enum-values": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/no-duplicate-enum-values": "error",
  },
});
bash
oxlint --deny typescript/no-duplicate-enum-values

版本

此规则添加于 v0.0.8。

参考资料