-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Description
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
Repro Code
function incorrectlyDetectingNeededAssertionWithVoidReturnVal(arg?: number): number | void {
if (arg) {
return arg;
}
}
const testError: number = incorrectlyDetectingNeededAssertionWithVoidReturnVal()!;
ESLint Config
module.exports = {
parser: "@typescript-eslint/parser",
rules: {
"@typescript-eslint/no-unnecessary-type-assertion": "error"
},
};
tsconfig
Expected Result
The rule should correctly detect the type assertion as necessary since the function may return void
and we are trying to assign the result of the function to a constant that is a number
.
Actual Result
Linter fails as it detects the type assertion as unnecessary. By omitting the non-null operator, TSC (correctly) reports an error (which you can see in a line below)
Additional Info
The rule works as expected if the function potentially returns null
or undefined
- it only reports false positives if the function returns void
. It also reports false positives when using class methods that potentially return void.
The rule also works correctly when checking the usage of non-null operator with variables / constants - it does not matter whether they are potentially null
, undefined
or void
.