TypeScript allows all sorts of expressions to initialize enum members. However, as enums create their own scope, using identifiers can lead to
unexpected results. The recommendation is thus to use only literal values when defining enum members.
What is the potential impact?
An enum member will shadow any variable defined with the same name in an enclosing scope. In the code example below, the day
member
value should derive from the hour
const variable. But it is computed using the enum hour
member instead, leading to the
result 24 instead of the expected 1440.
const hour = 3600; // hour in seconds
enum DurationInMinutes {
hour = 60, // hour in minutes
day = 24 * hour / 60 // Expecting hour in seconds but got hour in minutes
}