Skip to content

Bug: [no-unnecessary-type-parameters] Type parameters that use other type parameters and get only used once are often not unnecessary #11395

@TheBlue-1

Description

@TheBlue-1

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

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

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

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>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy