typescript-eslint icon indicating copy to clipboard operation
typescript-eslint copied to clipboard

Enhancement: [@typescript-eslint/strict-boolean-expressions] allowNullableEnum option

Open Hamusutaa opened this issue 2 years ago • 0 comments

Before You File a Proposal Please Confirm You Have Done The Following...

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

Hamusutaa avatar Sep 07 '22 16:09 Hamusutaa