跳转到内容

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.

enum Version {
V1,
}
code-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,
++++
enum Status {
Open = 1,
Close,
}
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,
++++
enum Color {
Red = "Red",
Green = "Green",
Blue,
}
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,
+++++++++
enum Status {
Open = 1,
Close = 2,
}
enum Color {
Red = "Red",
Green = "Green",
Blue = "Blue",
}
declare enum Weather {
Rainy,
Sunny,
}