Skip to content

Bug: [no-unnecessary-type-parameters] Counts type parameter references in body irrelevant to the signature #9735

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
JoshuaKGoldberg opened this issue Aug 6, 2024 · 5 comments · Fixed by #9738
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

@JoshuaKGoldberg
Copy link
Member

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=.tsx&code=GYVwdgxgLglg9mABMOcA8AVAfACgPoBciGAlIgN4BQiiECAzlIgB5EaIC8iYIANrwEIA3NVoMmATzadufQSIC%2BlIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWsRFCAtonyJoqDJCXQO0SODABfELqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function foo<T>(_: T) {
  const x: T = null!;
  const y: T = null!;
}

ESLint Config

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

tsconfig

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

Expected Result

T should be marked as an unnecessary type parameter. It's only used once in the type signature.

Actual Result

No report.

Additional Info

Repro case courtesy of @That-Guy977's #9664 (comment), thanks!

💖

@JoshuaKGoldberg JoshuaKGoldberg 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 Aug 6, 2024
@kirkwaiblinger kirkwaiblinger 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
@ST-DDT
Copy link
Contributor

ST-DDT commented Aug 19, 2024

@JoshuaKGoldberg Could you please explain why type parameter usages in the implementation should be ignored?
Neither the issue description nor the closing PR explain this.

Is const array: [T, number] = [value, 1] somehow bad?

@Josh-Cena
Copy link
Member

Josh-Cena commented Aug 19, 2024

Type parameters in the body is type checked using the base constraint of the parameter, so : T should be equivalent to : unknown. Type parameters are not realized until the function gets called, by which time the function body has been type checked already.

...In other words, if the rule reports, then you could remove the type parameter and rewrite the function body, and achieve a function that's as sound as before.

@That-Guy977
Copy link

Type parameters in the body is type checked using the base constraint of the parameter, so : T should be equivalent to : unknown.

Aren't they non-equivalent when assigning to T, acting more like never?

function f<T>() {
  const x: T = ""
//      ^
// Type 'string' is not assignable to type 'T'.
//   'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
}

@Josh-Cena
Copy link
Member

In a way, they are the widest possible type when assigned from and the narrowest possible type when assigned to. I realized how tricky it is so hopefully my second paragraph is a better phrasing of what I meant.

@bradzacher
Copy link
Member

Putting it another way:

A type parameter T bounded by extends U acts like a transparent1 nominal type.
It is only assignable to any, unknown, T or U
It only accepts any, never or T

Note: for unbounded type parameter (eg <T>) is the same as <T extends unknown> and thus U = unknown above.

Footnotes

  1. Side note that the only difference between a transparent nominal and an opaque nominal type is that an opaque nominal type disallows the assignment to U. This is why I like flow cos it lets you declare both

@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 28, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 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
6 participants