Skip to content

Bug: [no-unnecessary-type-assertion] false positive for generic functions with multiple signatures #10722

Open
@TkDodo

Description

@TkDodo

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.7.3&fileType=.tsx&code=MYewdgzgLgBCBGArAXDA3gXxgXnRgsAFBED0JMA7iAE4DWEMAhgwKYAeADi8FCwCZFQkWADdGAGwCuLBrgDySblAB0YqTIAUCRAEpB4aEwgQW1XnwBqE6bJhqbRmNGoBLMAHMA2gF0A3EVJyADMJExgOEAgXKBcRFn1hGD4XaiVxAE8AQWNTcyt1WwVEJVVrTW0dR2c3Lz8AwiA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QeoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const obj: {} = {}

// works as expected
const values = Object.values(obj)
const assertedValues = values as string[];

// false positive
const directlyAssertedValues = Object.values(obj) as string[];

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  }
}

tsconfig

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

Expected Result

There should be no error in the provided playground

Actual Result

There’s an error in line 8:

This assertion is unnecessary since it does not change the type of the expression. 8:32 - 8:62

Additional Info

It seems that when we have Object.values(obj) that returns an Array<unknown>, and we’d want to assert that to a more specific array, the rule thinks the assertion is unnecessary when it’s actually necessary.

Note that this doesn’t happen when we change obj to be Record<string, unknown>, so maybe this is specific to the type {}. I’m aware this type should generally be avoided, but I’m not sure how this makes a difference here.

The error can also be reproduced by inlining the object: Object.values({}).

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions