跳转到内容

useLiteralEnumMembers

Diagnostic Category: lint/style/useLiteralEnumMembers

Since: v1.0.0

Sources:

Require all enum members to be literal values.

Usually, an enum member is initialized with a literal number or a literal string. However, TypeScript allows the value of an enum member to be many different kinds of expressions. Using a computed enum member is often error-prone and confusing. This rule requires the initialization of enum members with constant expressions. It allows numeric and bitwise expressions for supporting enum flags. It also allows referencing previous enum members.

const x = 2;
enum Computed {
A,
B = x,
}
code-block.ts:4:9 lint/style/useLiteralEnumMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The enum member should be initialized with a literal value such as a number or a string.

2 │ enum Computed {
3 │ A,
> 4 │ B = x,
^
5 │ }
6 │

enum Direction {
Left,
Right,
}
enum Order {
Less = -1,
Equal = 0,
Greater = 1,
}
enum State {
Open = "Open",
Close = "Close",
}
enum FileAccess {
None = 0,
Read = 1,
Write = 1 << 1,
All = Read | Write
}