Skip to content

Bug: [no-unnecessary-boolean-literal-compare] should require strictNullChecks  #10706

Closed
@earshinov

Description

@earshinov

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=5.7.3&fileType=.ts&code=GYVwdgxgLglg9mABFApgZygCgIYH4BciARnHADYrZgCUhJ5lSA3gLABQiiATilCF0myIAhAF5RiYNjJoUAbnYBfdu1QZM1OUA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ARhw5IhTWo3yJoQ%2BLTIcAtsRGJ0UJdA7RI4MAF8QZoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYAZgIaonrhgC%2BIfkA&tokens=false

Repro Code

function test(a?: boolean): boolean {
  return a !== false;
}

test();

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strict": false
  }
}

Actual Result

With the configuration above (without strictNullChecks), the @typescript-eslint/no-unnecessary-boolean-literal-compare rule emits a (false) error. Applying the automatic fix translates

function test(a?: boolean): boolean {
  return a !== false;
}

into

function test(a?: boolean): boolean {
  return a;
}

which is obviously not equivalent as a is nullable.

Root cause seems to be that without strictNullChecks, TypeScript reports the type of a as boolean instead of boolean | undefined, making typescript-eslint think that a is not nullable ⇒ a !== false is equivalent to just a.

My guess is that the no-unnecessary-boolean-literal-compare rule absolutely has to check for strictNullChecks and refuse to work (like other rules do, such as prefer-nullish-coalescing).

We already broke our app in runtime after applying eslint --fix without reviewing each and every change. We can't enable strict / strictNullChecks as it would emit thousands of errors in the legacy codebase.

Expected Result

Either of
a. (best option) no error, like with strictNullChecks: true (I assume, not technically possible)
b. eslint refuses to work if the no-unnecessary-boolean-literal-compare rule is enabled, but strictNullChecks aren't

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions