Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[no-unnecessary-type-assertion] False positive with lazily initialized local variable #2423

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
fasttime opened this issue Aug 25, 2020 · 3 comments
Labels
fix: strictNullChecks issues that were fixed by turning on strictNullChecks package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@fasttime
Copy link

fasttime commented Aug 25, 2020

Not sure if or how this relates to #2248. In my case, the repro is quite simple.

Repro

.eslintrc.json

{
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "project": "./tsconfig.json"
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
        "@typescript-eslint/no-unnecessary-type-assertion": [
            "error"
        ]
    }
}

index.ts

let foo: string[];
Reflect.apply(() => foo = ['bar'], null, []);
console.log(foo![0]);

package.json

{
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "latest",
    "@typescript-eslint/parser": "latest",
    "eslint": "7.4.0",
    "typescript": "4.0.2"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "strictNullChecks": false
    }
}

Run with: npx eslint index.ts

Expected Result

No error.

Actual Result

Getting error This assertion is unnecessary since it does not change the type of the expression @typescript-eslint/no-unnecessary-type-assertion on line 3.

Versions

package version
@typescript-eslint/eslint-plugin 3.10.0
@typescript-eslint/parser 3.10.0
TypeScript 4.0.2
ESLint 7.4.0
node 14.8.0
@fasttime fasttime added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Aug 25, 2020
@bradzacher
Copy link
Member

I am unable to repro this against master.
Are you using strictNullChecks?

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party unable to repro issues that a maintainer was not able to reproduce and removed triage Waiting for team members to take a look labels Aug 31, 2020
@fasttime
Copy link
Author

I am not using strictNullChecks, and I am still getting the error after updating @typescript-eslint/eslint-plugin and @typescript-eslint/parser to version 4.0.1 (I didn't try cloning the repo and building the packages from master).
I updated the repro with complete versions of the relevant files, that should help reproduce the issue in case the previous repro was missing information.

@bradzacher bradzacher added fix: strictNullChecks issues that were fixed by turning on strictNullChecks working as intended Issues that are closed as they are working as intended and removed awaiting response Issues waiting for a reply from the OP or another party unable to repro issues that a maintainer was not able to reproduce labels Aug 31, 2020
@bradzacher
Copy link
Member

bradzacher commented Aug 31, 2020

Unfortunately the rule cannot correctly detect this case with strictNullChecks turned off.
This is by-design in TS itself - TS does not do use before assigned analysis on code without this compiler option. Because it doesn't do this analysis, our rule has no information to work from.

Note that with the option off, you can actually omit the non-null assertion with strictNullChecks, and the code is still compile-time safe and will not throw any TS errors.

You should turn the option on, as having it off invites a ridiculous amount of unsafety into your codebase.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fix: strictNullChecks issues that were fixed by turning on strictNullChecks package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants