typescript/prefer-enum-initializers Pedantic
它的作用
要求每个枚举成员的值都必须显式初始化。
为什么这很糟糕?
在枚举成员值很重要的项目中,允许枚举使用隐式值会在枚举随时间修改时引入 bug。
示例
以下是此规则的一个错误代码示例:
typescript
// 错误,`Close` 的值不是常量
enum Status {
Open = 1,
Close,
}以下是此规则的一个正确代码示例:
typescript
enum Status {
Open = 1,
Close = 2,
}如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"typescript/prefer-enum-initializers": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"typescript/prefer-enum-initializers": "error",
},
});bash
oxlint --deny typescript/prefer-enum-initializers版本
此规则已在 v0.3.2 中添加。
