Skip to content

[no-unnecessary-type-assertion] false positive on uninitialized variables #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mysticatea opened this issue Apr 22, 2019 · 2 comments · Fixed by #478
Closed

[no-unnecessary-type-assertion] false positive on uninitialized variables #453

mysticatea opened this issue Apr 22, 2019 · 2 comments · Fixed by #478
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mysticatea
Copy link
Member

mysticatea commented Apr 22, 2019

Repro

{
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
}
function test() {
    let resolve: () => void
    new Promise(resolve0 => {
        resolve = resolve0
    })
    return resolve!
}

Expected Result

No errors because it's a syntax error if I removed the !.

Actual Result

6:12  error  This assertion is unnecessary since it does not change the type of the expression  @typescript-eslint/no-unnecessary-type-assertion

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 1.7.0
@typescript-eslint/parser 1.7.0
TypeScript 3.4.3
ESLint 5.16.0
node 10.15.3
npm 6.9.0
@mysticatea mysticatea added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 22, 2019
@bradzacher
Copy link
Member

bradzacher commented Apr 22, 2019

I'm not sure that we can do anything about this.
The syntax error is because TS has decided the variable is uninitialised.
However "uninitialised" is not a type.
At the return statement, the variable has the type () => void, which is why there is an error - you're non-nulling a non-null type.
The operator is just telling ts to ignore the uninitialised state.

I haven't tested it, but I'd assume that the following code works:

function test() {
    let resolve: null | () => void = null
    new Promise(resolve0 => {
        resolve = resolve0
    })
    return resolve!
}

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party bug Something isn't working and removed triage Waiting for team members to take a look labels Apr 22, 2019
@mysticatea
Copy link
Member Author

mysticatea commented Apr 22, 2019

That rule has autofix and the autofix broke the code. ESLint must not fix code if it changed semantics. Generating syntax error is definitely a bug. Also, to tell the variable was actually initialized is a intentional feature of !. It's definitely not "unnecessary assertion" and the rule should not warn it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants