no-duplicate-enum-values
Disallow duplicate enum member values.
在 ESLint 配置 中扩展"plugin:@typescript-eslint/recommended"
可启用此规则。
尽管 TypeScript 支持重复的枚举成员值,但人们通常期望成员在同一枚举中具有唯一的值。 重复的值可能会导致难以追踪的错误。
英:Although TypeScript supports duplicate enum member values, people usually expect members to have unique values within the same enum. Duplicate values can lead to bugs that are hard to track down.
module.exports = {
"rules": {
"@typescript-eslint/no-duplicate-enum-values": "error"
}
};
示例
此规则不允许定义多个成员初始化为相同值的枚举。
英:This rule disallows defining an enum with multiple members initialized to the same value.
此规则仅对使用字符串或数字字面量初始化的枚举成员强制执行。 此规则不检查没有初始值设定项或使用表达式初始化的成员。
- ❌ 不正确
- ✅ 正确
enum E {
A = 0,
B = 0,
}
Open in Playgroundenum E {
A = 'A',
B = 'A',
}
Open in Playgroundenum E {
A = 0,
B = 1,
}
Open in Playgroundenum E {
A = 'A',
B = 'B',
}
Open in Playground何时不使用它
有时,对于非常特定的用例,包含重复的枚举成员可能很有用。 例如,在重命名枚举成员时,保留旧名称有时会很有用,直到计划的重大重大更改为止。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。
英:It can sometimes be useful to include duplicate enum members for very specific use cases. For example, when renaming an enum member, it can sometimes be useful to keep the old name until a scheduled major breaking change. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
一般来说,如果你的项目故意重复枚举成员值,则可以避免此规则。
英:In general, if your project intentionally duplicates enum member values, you can avoid this rule.
选项
该规则不可配置。