Skip to content

Bug: [no-unnecessary-type-assertion] false positive when casting changes the inferred type from a generic #5359

Closed as not planned
@DetachHead

Description

@DetachHead

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=4.7.2&sourceType=module&code=CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPADwAqANPAKoB8AFAG5QQCuIhlAlPALzXwkBYAFDCA9KIDmIFCBgBLMGniwEKJAHd4clNllxgypURTMAtgCNZFTPJQS6jFm3g3tErr3gmLs4cnRY5rA8OHi0AEThXFBK3pYw8OJ6SDCEagC0zCgyYCBoaLAAnukYhQAOIOkxaLIYcqhAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

declare const foo: <T, U>(value: U) => T

//generics are now inferred as <number, string>(value: string) => number
const bar = foo("") as number //error: no-unnecessary-type-assertion

ESLint Config

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

tsconfig

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

Expected Result

no error

Actual Result

error: no-unnecessary-type-assertion

Additional Info

sometimes it's useful to use a cast instead of specifying the generic, in case you only want to have to specify one type while still having the other types inferred.

Versions

package version
@typescript-eslint/eslint-plugin 5.30.7
@typescript-eslint/parser 5.30.7
TypeScript 4.7.2
ESLint 8.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: 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