Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/no-unsafe-enum-comparison/
Description
In our codebase, in various places we have values which are typed as string. This can be due to control/component limitations, or because they are coming from the database, or because they are partially user-defined (ie. there are standard values and custom values).
We place the "known values" or "standard values" in a string enum, and do comparisons with these known values to handle them, with the user-provided value on the left side and the known enum value on the right side.
This is now an error due to no-unsafe-enum-comparison
. At the same time, this rule has found places where we did incorrect comparisons, so we don't want to disable the rule.
The proposal would be to add an option to allow a comparison between a primitive value (string/number) on the left side, and an enum value of the same type on the right side. Any other comparisons would still be an error.
Fail
enum KnownVegetable {
Asparagus = 'asparagus',
}
declare let vegetable: KnownVegetable;
declare let apiVegetable: string;
vegetable === 'asparagus';
KnownVegetable.Asparagus === apiVegetable; // still not allowed: primitive value must be on left side
Pass
enum KnownVegetable {
Asparagus = 'asparagus',
}
declare let vegetable: KnownVegetable;
declare let apiVegetable: string;
vegetable === KnownVegetable.Asparagus;
apiVegetable === KnownVegetable.Asparagus; // now allowed: primitive value on left side compared with enum value on right side
Additional Info
No response