Skip to content

[strict-boolean-expressions] False negative for generic parameters #3644

@RunDevelopment

Description

@RunDevelopment
  • 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"
  }
}
function test<S>(): void {
	const foo = getEmptyArray<S>();
	if (foo.pop()) {
		throw new Error();
	}

	const bar = getEmptyArray<boolean>();
	if (bar.pop()) {
		throw new Error();
	}
}
function getEmptyArray<S>(): S[] {
	return [];
}

Expected Result

Both if (foo.pop()) and if (bar.pop()) should produce warnings. Both are potentially undefined and most likely not booleans. foo.pop() has the return type S | undefined and bar has number | undefined.

Actual Result

if (foo.pop()) does not produce a warning despite almost certainly not being a boolean.

I found that if the condition is a union with a type parameter (S | anything), then strict-boolean-expressions won't report anything. This is a problem because that means that the rule cannot check all conditions inside generic functions.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.28.4
@typescript-eslint/parser 4.23.0
TypeScript 4.2.4
ESLint 7.26.0
node 14.15.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpackage: 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