Skip to content

typescript/prefer-literal-enum-member Restriction

What it does

Explicit enum value must only be a literal value (string, number, boolean, etc).

Why is this bad?

TypeScript allows the value of an enum member to be many different kinds of valid JavaScript expressions. However, because enums create their own scope whereby each enum member becomes a variable in that scope, developers are often surprised at the resultant values.

Example

ts
const imOutside = 2;
const b = 2;
enum Foo {
  outer = imOutside,
  a = 1,
  b = a,
  c = b,
}

References

Released under the MIT License.