useEnumInitializers
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/style/useEnumInitializers
Since: v1.0.0
Sources:
Require that each enum member value be explicitly initialized.
TypeScript enums are a practical way to organize semantically related constant values. Members of enums that don’t have explicit values are by default given sequentially increasing numbers.
When the value of enum members are important, allowing implicit values for enum members can cause bugs if enum declarations are modified over time.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.ts:1:6 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This enum declaration contains members that are implicitly initialized.
> 1 │ enum Version {
│ ^^^^^^^
2 │ V1,
3 │ }
ℹ This enum member should be explicitly initialized.
1 │ enum Version {
> 2 │ V1,
│ ^^
3 │ }
4 │
ℹ Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.
ℹ Safe fix: Initialize all enum members.
2 │ ····V1·=·0,
│ ++++
code-block.ts:1:6 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This enum declaration contains members that are implicitly initialized.
> 1 │ enum Status {
│ ^^^^^^
2 │ Open = 1,
3 │ Close,
ℹ This enum member should be explicitly initialized.
1 │ enum Status {
2 │ Open = 1,
> 3 │ Close,
│ ^^^^^
4 │ }
5 │
ℹ Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.
ℹ Safe fix: Initialize all enum members.
3 │ ····Close·=·2,
│ ++++
code-block.ts:1:6 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This enum declaration contains members that are implicitly initialized.
> 1 │ enum Color {
│ ^^^^^
2 │ Red = “Red”,
3 │ Green = “Green”,
ℹ This enum member should be explicitly initialized.
2 │ Red = “Red”,
3 │ Green = “Green”,
> 4 │ Blue,
│ ^^^^
5 │ }
6 │
ℹ Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.
ℹ Safe fix: Initialize all enum members.
4 │ ····Blue·=·“Blue”,
│ +++++++++