Skip to content

prefer-enum-member

Replaces raw enum values with EnumName.Member references when the expected type points to a TypeScript enum.

This rule uses type information to find string and number literals that match a regular enum or const enum member. When it can resolve a single enum match, it reports the literal and fixes it to the member reference.

It also checks object keys when the key type comes from an enum, including shapes built with Record, mapped types, type aliases, and Readonly wrappers.

Incorrect
enum Color { Red = "Red", Blue = "Blue" }
function setColor(color: Color) {}
setColor("Red");
const palette: Record<Color, string> = {
Red: "#f00",
};
Correct
enum Color { Red = "Red", Blue = "Blue" }
function setColor(color: Color) {}
setColor(Color.Red);
const palette: Record<Color, string> = {
[Color.Red]: "#f00",
};
  • module specifiers like import \"./file\"
  • directive strings like "use strict"
  • literal types such as type T = "Red"
  • property keys handled in places where there is no enum-typed object shape to check against