Lint Rules

useEnumInitializers (since v11.0.0)

Require that each enum member value be explicitly initialized.

TypeScript enums are a practical way to organize semantically related constant values. Members of enums that don’t have explicit values are by default given sequentially increasing numbers.

When the value of enum members are important, allowing implicit values for enum members can cause bugs if enum declarations are modified over time.

Source: https://typescript-eslint.io/rules/prefer-enum-initializers

Examples

Invalid

enum Version {
    V1,
}
nursery/useEnumInitializers.js:2:5 lint/nursery/useEnumInitializers  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━

   The enum member should be initialized.
  
    1 │ enum Version {
  > 2 │     V1,
       ^^
    3 │ }
    4 │ 
  
   Allowing implicit values for enum members can cause bugs if enum declarations are modified over time.
  
   Suggested fix: 
  
    2 │ ····V1·=·0,
        ++++ 
enum Status {
    Open = 1,
    Close,
}
nursery/useEnumInitializers.js:3:5 lint/nursery/useEnumInitializers  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━

   The enum member should be initialized.
  
    1 │ enum Status {
    2 │     Open = 1,
  > 3 │     Close,
       ^^^^^
    4 │ }
    5 │ 
  
   Allowing implicit values for enum members can cause bugs if enum declarations are modified over time.
  
   Suggested fix: 
  
    3 │ ····Close·=·2,
           ++++ 
enum Color {
    Red = "Red",
    Green = "Green",
    Blue,
}
nursery/useEnumInitializers.js:4:5 lint/nursery/useEnumInitializers  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━

   The enum member should be initialized.
  
    2 │     Red = "Red",
    3 │     Green = "Green",
  > 4 │     Blue,
       ^^^^
    5 │ }
    6 │ 
  
   Allowing implicit values for enum members can cause bugs if enum declarations are modified over time.
  
   Suggested fix: 
  
    4 │ ····Blue·=·"Blue",
          +++++++++ 

Valid

enum Status {
    Open = 1,
    Close = 2,
}
enum Color {
    Red = "Red",
    Green = "Green",
    Blue = "Blue",
}