Skip to content
← Back to rules

typescript/prefer-enum-initializers Pedantic

💡 A suggestion is available for this rule.

What it does

Requires each enum member's value to be explicitly initialized.

Why is this bad?

In projects where enum member values matter, allowing enums to use implicit values can introduce bugs when the enum is modified over time.

Example

The following is an incorrect code example for this rule:

typescript
// Error, `Close`'s value is not constant
enum Status {
  Open = 1,
  Close,
}

The following is a correct code example for this rule:

typescript
enum Status {
  Open = 1,
  Close = 2,
}

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "typescript/prefer-enum-initializers": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "typescript/prefer-enum-initializers": "error",
  },
});
bash
oxlint --deny typescript/prefer-enum-initializers

Version

This rule was added in v0.3.2.

References