Skip to content

Bug: [no-unnecessary-type-parameters] double false positive #9524

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
abrahamguo opened this issue Jul 9, 2024 · 6 comments · Fixed by #9530
Closed
4 tasks done

Bug: [no-unnecessary-type-parameters] double false positive #9524

abrahamguo opened this issue Jul 9, 2024 · 6 comments · Fixed by #9530
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

@abrahamguo
Copy link
Contributor

abrahamguo commented Jul 9, 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=KYDwDg9gTgLgBAYwgOwM7wLYEMwHkBGAVnALxwA8A0nKDMMgCapzpQCWyA5gDRwBqvACoA%2BABQQiALjgBvOAG0A1sACecDnEoBdAPzS%2BcAL68AZsmmjlK6ZV4A3LABt9ASlLC4gtyWEAoOHAEhMAIMAB09DDswKjiRC5h2GCikVBqPnBmomE5qWpYzPK2-FouLgDcQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWsRFCAtonyJoqDJCXQO0SODABfELqA&tsconfig=&tokens=false

Repro Code

export const mapObj = <K extends string, V, T>(obj: { [key in K]?: V }, fn: (key: K, val: V) => T) =>
  Object.entries(obj).map(entry => fn(...entry as [K, V]));

ESLint Config

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

Expected Result

The rule should not report on VV is used 3 times

Actual Result

The rule claims that V is only used once, when it's actually used 3 times.

Additional Info

No response

@abrahamguo abrahamguo 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 9, 2024
@bradzacher
Copy link
Member

The message is bad but the report is kind of correct.

You can rewrite this to remove a parameter

export const mapObj =
  <Obj extends object, T>(
    obj: Obj,
    fn: (key: keyof Obj, val: Obj[keyof Obj]) => T,
  ) =>
    Object.entries(obj)
      .map(entry => fn(...entry as [keyof Obj, Obj[keyof Obj]]));

mapObj(
  { a: "", b: 1 },
  (
    k,
 // ^? "a" | "b"
    v
 // ^? string | number
  ) => true
)

playground

@danvk
Copy link
Collaborator

danvk commented Jul 9, 2024

While the type parameter may not strictly be needed, this rule is nowhere near smart enough to figure that out. :) The issue is that visitType isn't descending into all parts of a mapped type. Fix coming up…

@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 Jul 9, 2024
@abrahamguo
Copy link
Contributor Author

@danvk since the rule reports that the type parameter is only used once, but it is actually used three times, does that mean that two occurences of the type parameter have been missed?

If one missed occurrence was in the mapped type, what is the other missed occurrence?

@danvk
Copy link
Collaborator

danvk commented Jul 9, 2024

@abrahamguo this is a reduced version of your code (playground):

declare function mapObj<
  K extends string,
  V,  // Type parameter V is used only once
>(
  obj: { [key in K]: V },
  fn: (key: K, val: V) => number,
): number[];

I've removed the function implementation because it's not relevant to this rule. V is declared once and used twice, so it appears a total of three times. So this is considered valid with my change, but not valid if you drop the use in { [key in K]: V }.

@abrahamguo
Copy link
Contributor Author

abrahamguo commented Jul 9, 2024

Ah. So the usage of V in the type assertion in the function implementation body (as [K, V]) doesn't count as a "usage" according to this lint rule?

@danvk
Copy link
Collaborator

danvk commented Jul 9, 2024

No. Type parameters are used to relate types in a type signature. If they only appear once in the type signature (other than their declaration), then they're not relating anything. The implementation of a function can only affect its type signature if it has an inferred return type, which this rule specifically checks.

@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 Jul 23, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 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
4 participants