Skip to content

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.

enum Foo {
a = 1,
b = a,
}
const x = 1;
enum Foo {
a = x,
}
enum Foo {
a = 1 + 2,
}
enum Foo {
a = getValue(),
}

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.