typescript-eslint
typescript-eslint copied to clipboard
Enhancement: [@typescript-eslint/strict-boolean-expressions] allowNullableEnum option
Before You File a Proposal Please Confirm You Have Done The Following...
- [X] I have searched for related issues and found none that match my proposal.
- [X] I have searched the current rule list and found no rules that match my proposal.
- [X] I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- [X] 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/strict-boolean-expressions/
Description
I propose that a new option be added to the @typescript-eslint/strict-boolean-expressions
rule: allowNullableEnum
, which would behave similarly to the allowNullableNumber
option, but for Enums which contain a zero as one of their possible values. It could default to true in order to remain backwards-compatible, but allow being set false in order to catch potential issues where one of the valid values of the Enum is falsy.
Fail
enum ExampleEnum {
This = 0,
That = 1,
}
const rand = Math.random();
let theEnum: ExampleEnum | null = null;
if (rand < .3) {
theEnum = ExampleEnum.This;
} else if (rand < .7) {
theEnum = ExampleEnum.That;
}
// No warning thrown
if (theEnum) {
console.log('That!');
}
Pass
enum ExampleEnum {
This = 0,
That = 1,
}
const rand = Math.random();
let theEnum: ExampleEnum | null = null;
if (rand < .3) {
theEnum = ExampleEnum.This;
} else if (rand < .7) {
theEnum = ExampleEnum.That;
}
if (theEnum === ExampleEnum.That) {
console.log('That!');
}
Additional Info
No response