Skip to main content

typescript/prefer-enum

Prefer enums over string literal comparisons and unions.

Targeted pattern scope

⚠️ This rule requires type information to run. Configure type-aware linting (parserOptions.project or projectService) before enabling it.

This rule reports three patterns:

  • string literal comparisons against enum-like expressions,
  • string literal return values from enum-like return contexts,
  • type aliases that are pure unions of multiple string literals.

What this rule reports

This rule reports opportunities to replace string-literal-based state modeling with enum members.

Why this rule exists

Enums centralize allowed values and reduce drift between string literals spread across comparisons, returns, and type unions.

❌ Incorrect

type Status = "open" | "closed";
// ❌ reported (pure string-literal union)

✅ Correct

enum Status {
Open = "open",
Closed = "closed",
}

Behavior and migration notes

This rule reports only and does not provide an autofix.

Because it uses type analysis for enum-like checks, ensure parser services are enabled in lint configuration.

Options

This rule has no options.

Additional examples

enum Status {
Open = "open",
Closed = "closed",
}

const getStatus = (): Status => {
return "open";
};
// ❌ reported (string literal return in enum-like return context)

ESLint flat config example

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/typescript/prefer-enum": "error",
},
},
];

When not to use it

Disable this rule if string literal unions are preferred over enums in your project's type design.

Package documentation

Rule catalog ID: R105

Further reading

Adoption resources

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.