no-unsafe-enum-comparison
Disallow comparing an enum value with a non-enum value.
Extending "plugin:@typescript-eslint/recommended-type-checked"
in an ESLint configuration enables this rule.
Some problems reported by this rule are manually fixable by editor suggestions.
This rule requires type information to run.
The TypeScript compiler can be surprisingly lenient when working with enums. While overt safety problems with enums were resolved in TypeScript 5.0, some logical pitfalls remain permitted. For example, it is allowed to compare enum values against non-enum values:
enum Vegetable {
Asparagus = 'asparagus',
}
declare const vegetable: Vegetable;
vegetable === 'asparagus'; // No error
The above code snippet should instead be written as vegetable === Vegetable.Asparagus
.
Allowing non-enums in comparisons subverts the point of using enums in the first place.
By enforcing comparisons with properly typed enums:
- It makes a codebase more resilient to enum members changing values.
- It allows for code IDEs to use the "Rename Symbol" feature to quickly rename an enum.
- It aligns code to the proper enum semantics of referring to them by name and treating their values as implementation details.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-enum-comparison": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-enum-comparison": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
enum Fruit {
Apple,
}
declare let fruit: Fruit;
// bad - comparison between enum and explicit value instead of named enum member
fruit === 0;
enum Vegetable {
Asparagus = 'asparagus',
}
declare let vegetable: Vegetable;
// bad - comparison between enum and explicit value instead of named enum member
vegetable === 'asparagus';
declare let anyString: string;
// bad - comparison between enum and non-enum value
anyString === Vegetable.Asparagus;
Open in Playgroundenum Fruit {
Apple,
}
declare let fruit: Fruit;
fruit === Fruit.Apple;
enum Vegetable {
Asparagus = 'asparagus',
}
declare let vegetable: Vegetable;
vegetable === Vegetable.Asparagus;
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
If you don't mind enums being treated as a namespaced bag of values, rather than opaque identifiers, you likely don't need this rule.
Sometimes, you may want to ingest a value from an API or user input, then use it as an enum throughout your application. While validating the input, it may be appropriate to disable the rule. Alternately, you might consider making use of a validation library like Zod. See further discussion of this topic in #8557.
Finally, in the rare case of relying on an third party enums that are only imported as type
s, it may be difficult to adhere to this rule.
You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.