Skip to content

Bug: [no-unnecessary-type-assertion] false positive when used with a any type coming from a generic #11298

Closed as duplicate of#10722
@julienw

Description

@julienw

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.8.2&fileType=.tsx&code=FAEwpgxgNghgTmABAMwK4DsIBcCWB7dRVAZzABk8Zw4ARGLGAHgBVEBeRGdATwD4AKAJQAuRMwDcwYBALEsiAN4o8eRAF92RUhSpha9GEM7FFyvKLlwc6AObrJQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RIAGnBgAviD1A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

declare function useLoaderData<T = any>(): T;

const { foo } = useLoaderData() as { foo: string };

ESLint Config

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

tsconfig

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

Expected Result

No error.

Actual Result

False positive.

Additional Info

I use useLoaderData from the react-router package.

I could rewrite the code like this:

declare function useLoaderData<T = any>(): T;

const { foo } = useLoaderData<{ foo: string }>();

But this is functionally equivalent, and I think the rule shouldn't error in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingduplicateThis issue or pull request already existslocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: 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

      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