Skip to content

[no-unnecessary-boolean-literal-compare] Breaks type when using allowComparingNullableBooleansToTrue #3783

Description

@paleite
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

Playground

{
  "rules": {
    "@typescript-eslint/no-unnecessary-boolean-literal-compare": [
      "error",
      {
        allowComparingNullableBooleansToTrue: false,
        allowComparingNullableBooleansToFalse: false,
      },
    ],
  }
}
const isTrue = (x: boolean | undefined): boolean => x === true;

Expected Result

I would've expected either one of the following fixes to be output

// fall back to false, since undefined is falsey
const isTrue = (x: boolean | undefined): boolean => x ?? false;
// coerce to boolean
const isTrue = (x: boolean | undefined): boolean => !!x;
// convert to boolean
const isTrue = (x: boolean | undefined): boolean => Boolean(x);
// leave as-is
const isTrue = (x: boolean | undefined): boolean => x === true;

Actual Result

const isTrue = (x: boolean | undefined): boolean => x;

Additional Info

The TypeScript compiler now gives an error:

Type 'boolean | undefined' is not assignable to type 'boolean'.
  Type 'undefined' is not assignable to type 'boolean'. ts(2322)

Versions

package version
@typescript-eslint/eslint-plugin 4.29.2
@typescript-eslint/parser 4.29.2
TypeScript 4.3.5
ESLint 7.30.0
node 14.16.1

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

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions