Description
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
Repro Code
/** uses a fallback when a value is not set on an object */
function notSetFallback<
T,
K extends string,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- removing this changes the return type
O extends (Partial<Record<K, T>> & Record<string, unknown>) | undefined
>(obj: O, key: K, fallback: T): T | undefined {
return obj && key in obj ? obj[key] : fallback;
}
function notSetFallbackWithoutObjectType<T, K extends string>(
obj: (Partial<Record<K, T>> & Record<string, unknown>) | undefined,
key: K,
fallback: T
): T | undefined {
return obj && key in obj ? obj[key] : fallback;
}
function tester(fallbackString: string) {
notSetFallback({ a: true, b: "hi" }, "b", fallbackString); // ok
notSetFallbackWithoutObjectType({ a: true, b: "hi" }, "b", fallbackString); // error
const obj = { a: true, b: "hi" } as Partial<{
a: boolean, b: string
}>
const stringValue1: string | undefined = notSetFallback(obj, "b", fallbackString); // ok
const stringValue2: string | undefined = notSetFallbackWithoutObjectType(obj, "b", fallbackString); // error (return value typed "string | boolean | undefined")
}
ESLint Config
module.exports = {
parser: "@typescript-eslint/parser",
rules: {
"@typescript-eslint/no-unnecessary-type-parameters": "error",
},
};
tsconfig
Expected Result
I would expect the rule not to report cases where the type parameter is not unncecesarry like here.
I am not sure if that covers it but probably i would not expect the rule to not report a type parameter that uses another type parameter that is used at least twice in the function declaration.
Actual Result
The rule reported the type parameter to be unnecessary although it did change the behaviour of the function and also change the return type.
Additional Info
No response
edited:
Partial<Record<K, T>>
to (Partial<Record<K, T>> & Record<string, unknown>)