useLiteralEnumMembers (since v12.1.0)
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 literal values. It allows bitwise expressions for supporting enum flags.
In contrast to the equivalent ESLint rule, this rule allows arbitrary bitwise constant expressions.
Source: https://typescript-eslint.io/rules/prefer-literal-enum-member/
Examples
Invalid
const x = 2;
enum Computed {
A,
B = x,
}
nursery/useLiteralEnumMembers.js:4:9 lint/nursery/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 │
const x = 2;
enum Invalid {
A,
B = 2**3,
}
nursery/useLiteralEnumMembers.js:4:9 lint/nursery/useLiteralEnumMembers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ The enum member should be initialized with a literal value such as a number or a string.
2 │ enum Invalid {
3 │ A,
> 4 │ B = 2**3,
│ ^^^^
5 │ }
6 │
Valid
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 = 1 | (1 << 1)
}