Skip to main content

prefer-enum-initializers

Require each enum member value to be explicitly initialized.

💡

Some problems reported by this rule are manually fixable by editor suggestions.

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.

In projects where the value of enum members are important, allowing implicit values for enums can cause bugs if enums are modified over time.

This rule recommends having each enum member value explicitly initialized.

.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/prefer-enum-initializers": "error"
}
};

Try this rule in the playground ↗

Examples

enum Status {
Open = 1,
Close,
}

enum Direction {
Up,
Down,
}

enum Color {
Red,
Green = 'Green',
Blue = 'Blue',
}
Open in Playground

Options

This rule is not configurable.

When Not To Use It

If you don't care about enums having implicit values you can safely disable this rule.

Resources