@typescript-eslint/prefer-enum-initializers
Require every enum member to have an explicit value
has suggestions
Also called typescript-eslint/prefer-enum-initializers, typescript/prefer-enum-initializers
An enum member without a value gets a number from its position. The first is 0, and each one after that is one more than the member before it. If you add a member in the middle or sort the list, every member after it gets a new number. Values that were saved in a database or sent to another service now mean something else.
Each member without a value is reported.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-enum-initializers" = "error" # or "warn"Examples#
example.ts3 problems
enum Status {
DraftThe value of the member Draft should be explicitly defined.,
PublishedThe value of the member Published should be explicitly defined.,
ArchivedThe value of the member Archived should be explicitly defined.,
}example.tsNo problems
enum Status {
Draft = 0,
Published = 1,
Archived = 2,
}
enum Role {
Admin = "admin",
Member = "member",
}When not to use it#
If the values never leave the program and you only compare members with each other, the numbers don't matter.