Urara-Blog/node_modules/.pnpm-store/v3/files/d9/88c60b0bc7033b1eb651c4a347ef9071248dbb897b6b2f275d0e4ed86d49c128dadec24e2a6d0fd25456481efa9680cb0c2c3a9685cea449e7847a686a1dc6
2022-08-14 01:14:53 +08:00

93 lines
1.5 KiB
Text

---
description: 'Require each enum member value to be explicitly initialized.'
---
> 🛑 This file is source code, not the primary documentation location! 🛑
>
> See **https://typescript-eslint.io/rules/prefer-enum-initializers** for documentation.
This rule recommends having each `enum`s member value explicitly initialized.
`enum`s are a practical way to organize semantically related constant values. However, by implicitly defining values, `enum`s can lead to unexpected bugs if it's modified without paying attention to the order of its items.
## Rule Details
`enum`s infers sequential numbers automatically when initializers are omitted:
```ts
enum Status {
Open, // infer 0
Closed, // infer 1
}
```
If a new member is added to the top of `Status`, both `Open` and `Closed` would have its values altered:
```ts
enum Status {
Pending, // infer 0
Open, // infer 1
Closed, // infer 2
}
```
Examples of code for this rule:
<!--tabs-->
### ❌ Incorrect
```ts
enum Status {
Open = 1,
Close,
}
enum Direction {
Up,
Down,
}
enum Color {
Red,
Green = 'Green'
Blue = 'Blue',
}
```
### ✅ Correct
```ts
enum Status {
Open = 'Open',
Close = 'Close',
}
enum Direction {
Up = 1,
Down = 2,
}
enum Color {
Red = 'Red',
Green = 'Green',
Blue = 'Blue',
}
```
## Options
```jsonc
// .eslintrc.json
{
"rules": {
"@typescript-eslint/prefer-enum-initializers": "warn"
}
}
```
This rule is not configurable.
## When Not To Use It
If you don't care about `enum`s having implicit values you can safely disable this rule.