Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-unnecessary-type-assertion] False positive with value that can have the value null #1199

Closed
mdouglass opened this issue Nov 12, 2019 · 2 comments 路 Fixed by #1201
Closed
Assignees
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

@mdouglass
Copy link

Ok, I know I already tried to open this as #1195, but I totally borked that report. Hopefully, I'm not doing it again 馃, the issue was with no-unnecessary-type-assertion all along.

Repro

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": { "project": "./tsconfig.json", "sourceType": "module" },
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
}
function testFunction(_param: string | undefined): void { /* noop */ }
const value = 'test' as string | null | undefined
testFunction(value!)

Expected Result
No errors

Actual Result
3:14 error This assertion is unnecessary since the receiver accepts the original type of the expression @typescript-eslint/no-unnecessary-type-assertion

Additional Info
With the ! this compiles, but if the ! is removed, then you get a compiler error:

Argument of type 'string | null | undefined' is not assignable to parameter of type 'string | undefined'.
  Type 'null' is not assignable to type 'string | undefined'.ts(2345)

Versions

package version
@typescript-eslint/eslint-plugin 2.6.1
@typescript-eslint/parser 2.6.1
TypeScript 3.7.2
ESLint 6.6.0
node 13.1.0
npm 6.12.1
@mdouglass mdouglass added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Nov 12, 2019
@bradzacher
Copy link
Member

This report makes more sense :)

This is actually something I found a while ago but never got around to fixing.
I'll see if I can put up a PR today.

@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels Nov 12, 2019
@bradzacher bradzacher self-assigned this Nov 12, 2019
@mdouglass
Copy link
Author

Thank goodness :)
And thanks for taking a look at it!

@bradzacher bradzacher added the has pr there is a PR raised to close this label Nov 13, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
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