Skip to content

useLiteralEnumMembers (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"style": {
"useLiteralEnumMembers": "error"
}
}
}
}

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 members initialized with computed values are error-prone and confusing, because their value is only known when the code runs.

ℹ Replace the initializer with a literal number or string. Numeric and bitwise expressions, and references to previous members of the same enum, are also allowed.

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
}