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

Skip to content

[no-unnecessary-type-assertion] False positive with flow-based narrowing on generic types #3310

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

Open
3 tasks done
taymonbeal opened this issue Apr 24, 2021 · 4 comments
Open
3 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@taymonbeal
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "plugins": ["@typescript-eslint"],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "sourceType": "module",
    "project": ["./tsconfig.json"]
  },
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": ["error"]
  }
}
export function nonNullish<T>(value: T): NonNullable<T> {
  if (value == null) {
    throw new TypeError();
  }
  return value!;
}
{ "compilerOptions": { "strict": true } }

Expected Result

No errors. (Note that removing the ! causes TypeScript to complain.)

Actual Result

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

Additional Info

I think this isn't a duplicate of #2248 because I checked on ts-ast-viewer and any didn't show up anywhere.

Versions

package version
@typescript-eslint/eslint-plugin 4.22.1-alpha.11
@typescript-eslint/parser 4.22.0
TypeScript 4.2.4
ESLint 7.25.0
node 12.21.0
@taymonbeal taymonbeal added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 24, 2021
@bradzacher
Copy link
Member

The issue here is that typeof value === T - a generic type.
A generic type is "not a nullish type" - it is just an amorphous blob thing.
Which our code doesn't know how to handle.


Another way to implement this is as follows, which no longer requires the non-null assertion.

function nonNullish<T>(value: T | null | undefined): T {
  if (value == null) {
    throw new TypeError();
  }
  return value;
}

@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels Apr 24, 2021
@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@Josh-Cena
Copy link
Member

I have a similar case which I think is in the same vein as this one.

function foo<T extends string | number>(val: T) {
  const calculated = typeof val === "string" ? 0: (val as number);
  return calculated !== undefined ? 0.6 * calculated : 0;
}

The val type is T, and it's not subject to control flow narrowing, so if I remove the as number, 0.6 * calculated will fail.

@Josh-Cena Josh-Cena changed the title [no-unnecessary-type-assertion] False positive with flow-based narrowing [no-unnecessary-type-assertion] False positive with flow-based narrowing on generic types Jun 2, 2024
@taymonbeal

This comment has been minimized.

@bradzacher

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

4 participants