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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-unnecessary-type-assertion] Exhaustiveness check in TS requires ! to "reset" the never type #1282

Closed
Skalman opened this issue Nov 28, 2019 · 3 comments
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@Skalman
Copy link

Skalman commented Nov 28, 2019

Repro

{
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
}
type A = { kind: "abc" } | { kind: "def" };

function f(a: A) {
  switch (a.kind) {
    case "abc":
      return 1;
    case "def":
      return 2;
    default:
      // This `!` is necessary:
      throw new Error("Bad kind: " + a!?.kind);
  }
}

Expected Result
No error, since the ! is necessary.

Actual Result

"This assertion is unnecessary since it does not change the type of the expression @typescript-eslint/no-unnecessary-type-assertion"

Versions

package version
@typescript-eslint/eslint-plugin 2.9.0
@typescript-eslint/parser 2.9.0
TypeScript 3.7.2
ESLint 6.7.1
node 12.10.0
yarn 1.19.2
@Skalman Skalman added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Nov 28, 2019
@Skalman Skalman changed the title [no-unnecessary-type-assertion] Exhaustiveness check in TS requires ! [no-unnecessary-type-assertion] Exhaustiveness check in TS requires ! to "reset" the never type Nov 28, 2019
@bradzacher
Copy link
Member

Hmm. This is a really really weird behaviour within the typescript typechecker.
I'm not sure if we can detect this.

Without the non-null assertion, the type of a is never, however, with the operator, typescript instead reports the type of a as A.

Better described by the on-hover information:
image

image

The reason the rule reports on the usage of the !, is because typescript tells us the type is A, which is non-nullable.

This probably deserves a bug report of some kind on the typescript side..

@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels Nov 29, 2019
@Skalman
Copy link
Author

Skalman commented Nov 29, 2019

Huh, weird. Would you like me to report the TypeScript bug?

Edit: Done, see ref below.

@armano2 armano2 added the external This issue is with another package, not typescript-eslint itself label Jan 20, 2020
@bradzacher
Copy link
Member

microsoft/TypeScript#35431 has been closed and the fix will be released with the next TS version

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants