Skip to content

Bug: [no-unnecessary-type-assertion] false positive when casting changes the inferred type from a generic #5359

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
4 tasks done
DetachHead opened this issue Jul 21, 2022 · 3 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@DetachHead
Copy link

DetachHead commented Jul 21, 2022

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.7.2&sourceType=module&code=CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPADwAqANPAKoB8AFAG5QQCuIhlAlPALzXwkBYAFDCA9KIDmIFCBgBLMGniwEKJAHd4clNllxgypURTMAtgCNZFTPJQS6jFm3g3tErr3gmLs4cnRY5rA8OHi0AEThXFBK3pYw8OJ6SDCEagC0zCgyYCBoaLAAnukYhQAOIOkxaLIYcqhAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

declare const foo: <T, U>(value: U) => T

//generics are now inferred as <number, string>(value: string) => number
const bar = foo("") as number //error: no-unnecessary-type-assertion

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
};

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

no error

Actual Result

error: no-unnecessary-type-assertion

Additional Info

sometimes it's useful to use a cast instead of specifying the generic, in case you only want to have to specify one type while still having the other types inferred.

Versions

package version
@typescript-eslint/eslint-plugin 5.30.7
@typescript-eslint/parser 5.30.7
TypeScript 4.7.2
ESLint 8.15.0
@DetachHead DetachHead added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 21, 2022
@Josh-Cena
Copy link
Member

Josh-Cena commented Jul 21, 2022

Try document.querySelectorAll<HTMLImageElement>("")? Assertions are inference sites so they impact the generic function's type.

@DetachHead DetachHead changed the title Bug: [no-unnecessary-type-assertion] false positive when casting NodeListOf Bug: [no-unnecessary-type-assertion] false positive when casting changes the inferred type from a generic Jul 21, 2022
@DetachHead
Copy link
Author

that worked, thanks! interesting i had no idea casting effected generic inference.

in that case i've updated the issue with a better example

@bradzacher
Copy link
Member

bradzacher commented Jul 21, 2022

Yup, Josh is right.

When a generic Is only used in the return type then TS will propagate the asserting into the generic.

Using a generic in a return type is generally a code smell and breaks the golden rule of generics (a generic in a function should be used at least twice) https://effectivetypescript.com/2020/08/12/generics-golden-rule/


Check out thus example which shows how TS "backfills" the generic based on the assertion

declare function foo<T>(): T;

foo<string>();
foo() as string;

playground

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2022
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed bug Something isn't working triage Waiting for team members to take a look labels Jul 21, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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

3 participants