Skip to content

[strict-boolean-expressions] False positive when using union types #2894

Closed
@eamodio

Description

@eamodio
  • 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

{
  "rules": {
    "@typescript-eslint/strict-boolean-expressions": [
      "warn",
      {
	"allowString": true,
	"allowNumber": true,
	"allowNullableObject": true,
	"allowNullableBoolean": true,
	"allowNullableNumber": false,
	"allowNullableString": true,
	"allowAny": false
      }
    ],
  }
}
function foo(value?: boolean | { prop: any }) {
	if (value) {
		// value above errors with "Unexpected value in conditional. A boolean expression is required."
	}
}

tsconfig.json

{
	"compilerOptions": {
		"esModuleInterop": true,
		"experimentalDecorators": true,
		"forceConsistentCasingInFileNames": true,
		"incremental": true,
		"isolatedModules": true,
		"lib": ["es2019"],
		"module": "esnext",
		"moduleResolution": "node",
		"noFallthroughCasesInSwitch": true,
		"noImplicitReturns": true,
		"noUnusedLocals": false,
		"outDir": "dist",
		"resolveJsonModule": true,
		"rootDir": "src",
		"skipLibCheck": true,
		"sourceMap": true,
		"strict": true,
		"target": "es2019"
	}
}

Expected Result

No error on the if (value) { line

Actual Result

"Unexpected value in conditional. A boolean expression is required." on the if (value) { line

Versions

package version
@typescript-eslint/eslint-plugin 4.10.0
@typescript-eslint/parser 4.10.0
TypeScript 4.0.5
ESLint 7.16.0
node 12.16.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions