enumMemberLiterals
Requires all enum members to be literal values.
✅ This rule is included in the ts logicalStrict presets.
TypeScript allows enum members to be initialized with various expressions. However, using computed values can lead to unexpected results because enums create their own scope where each enum member becomes a variable.
For example, in this code:
const imOutside = 2;enum Foo { a = 1, b = a, c = imOutside,}Foo.b will be 1 (referencing Foo.a), but Foo.c will also be 1 because imOutside is shadowed by the auto-incremented value.
This rule requires all enum members to use literal values (strings or numbers) to prevent such confusion.
Examples
Section titled “Examples”enum Foo { a = 1, b = a,}const x = 1;enum Foo { a = x,}enum Foo { a = 1 + 2,}enum Foo { a = getValue(),}enum Foo { a = 1, b = 2,}enum Foo { a = "hello", b = "world",}enum Foo { a = -1, b = +2,}enum Foo { a, b, c,}Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you want to use computed expressions as enum values and understand the scoping implications, you may prefer to disable this rule. Some projects intentionally use enums as “opaque” entities where the values are not important.