useEnumInitializers
诊断类别:lint/style/useEnumInitializers
¥Diagnostic Category: lint/style/useEnumInitializers
自从:v1.0.0
¥Since: v1.0.0
来源:
¥Sources:
要求每个枚举成员值都明确初始化。
¥Require that each enum member value be explicitly initialized.
TypeScript 枚举是一种组织语义相关常量值的实用方法。没有明确值的枚举成员默认被赋予连续增加的数字。
¥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
¥Invalid
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,
│ ++++
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”,
│ +++++++++
¥Valid
¥Related links