-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Comments
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
) |
While the type parameter may not strictly be needed, this rule is nowhere near smart enough to figure that out. :) The issue is that |
@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? |
@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. |
Ah. So the usage of |
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. |
Uh oh!
There was an error while loading. Please reload this page.
Before You File a Bug Report Please Confirm You Have Done The Following...
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
ESLint Config
Expected Result
The rule should not report on
V
—V
is used 3 timesActual Result
The rule claims that
V
is only used once, when it's actually used 3 times.Additional Info
No response
The text was updated successfully, but these errors were encountered: