Skip to content

Bug: [strict-boolean-expressions] allowNullableEnum doesn't work with string enums #6583

Closed
@miluoshi

Description

@miluoshi

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.9.3&sourceType=module&code=PTAEDkFcFsCMFMBOp4DsYCg01AUQB4CG0ADgDby7rRRxKgDeGooAKgBYCWAzqALygADABpmbdoQAu-UAEZRAXwwBjAPapu0xIVQATWTICyU9gDpte1dAAUASgDcGCtMntK1WQC48RUhSowtAjIAD6g6GRkMhFkjpwAZqDWFvqgADxCpgDMtoxiru4wBgIExOSFNDDBphw8jkoJSQUB0LK5TEoYIKAAypKInKgA5ijUWNQ+Zf7UfQPDeSy1vAIA5IIroosS0quyGxhKahpaOroATEYm5qdWdo7OoM3UZ96lfhWzgyNhMdGQkXFEslThcMoJsu18m4WhcSr5yi1PsMalxuPUMI1rE8YGdIQogA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6ZfKsugIwHs+SAIZN6AD2LQUySnyaoMAbUgB3IdCaQANJihD48PioByCeEJ5IAok1gBbdGE6xEYAL4BdcO5BugA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

// Number enum
enum ExampleEnumNumber {
  This = 0,
  That = 1,
}
const rand1 = Math.random();
let theEnum1: ExampleEnumNumber | null = null;
if (rand1 < 0.3) {
  theEnum1 = ExampleEnumNumber.This;
}
if (theEnum1) {
}

// String enum
enum ExampleEnumString {
  This = '0',
  That = '1',
}
const rand2 = Math.random();
let theEnum2: ExampleEnumString | null = null;
if (rand2 < 0.3) {
  theEnum2 = ExampleEnumString.This;
}
if (theEnum2) {
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  {
    rules: {
      "@typescript-eslint/strict-boolean-expressions": ["warn", { "allowNullableEnum": true }]
    }
  }
};

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

The if (theEnum2) { line should not report warning.

Actual Result

Warning from strict-boolean-expressions rule with { allowNullableEnum: true } option appears.

Additional Info

PR that implemented the feature: #6096

From the code added in the PR it appears that added test doesn't check a case with { "allowNullableEnum": true } and code with a string enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions