Skip to content

Bug: [no-unnecessary-type-parameters] misleading error messages #9664

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
kirkwaiblinger opened this issue Jul 29, 2024 · 2 comments · Fixed by #9737
Closed
4 tasks done

Bug: [no-unnecessary-type-parameters] misleading error messages #9664

kirkwaiblinger opened this issue Jul 29, 2024 · 2 comments · Fixed by #9737
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kirkwaiblinger
Copy link
Member

kirkwaiblinger commented Jul 29, 2024

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=5.5.2&fileType=.ts&code=GYVwdgxgLglg9mABABwIYCcDOBTAUphAHgBVFsAPKbMAE00UynRjAHMA%2BACheRCgC4GTFqwCUg0gG8AUIkTpsUEOiS4AygHkAcgDo0WbNzC8ooxKnrEA3NIC%2BQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWsRFCAtonyJoqDJCXQO0SODABfELqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A&tokens=false

Repro Code

// T is used twice - maybe it should say "once in the signature"?
function parseJson<T extends string>(input: string): T {
  return JSON.parse(input) as T;
}

// T is used zero times
function parseJson<T extends string>(input: string): any {
  return JSON.parse(input) as any;
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unnecessary-type-parameters": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

I expect the error message should reflect correctly the number of times the type parameter is used.

Actual Result

"Type parameter T is used only once." in both cases

Additional Info

Let's also include a doc change to the docs page, if counting parameters is only supposed to be counting the usages in the signature?

@kirkwaiblinger kirkwaiblinger 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 29, 2024
@That-Guy977
Copy link

The current behavior does analyze the body to some extent.

  function bar<T>(_: T) {
    const x: T = /* ... */
+   const y: T = /* ... */
  }

Without the highlighted line, Type parameter T is used only once. is reported, but adding it suppresses the error. Playground

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Aug 6, 2024
@JoshuaKGoldberg
Copy link
Member

+ const y: T = /* ... */

That ... might be a bug in the rule. I don't think it's intentional that anything in the function body that's not a part of the signature should change how the rule interprets the signature. Filed #9735.

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Aug 20, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants