Skip to content

Bug: [no-unnecessary-type-assertion] False positive when using generic parameter with default #5487

Closed as not planned
@srmagura

Description

@srmagura

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.4&sourceType=module&code=C4TwDgpgBAggxsAlgewHYB4AqA+KBeKAbyjgENgIBzZAJxAC4pMoBfAKA4DMBXVBFVFAC2IAGK84WfFFTchAIwg1sAClL80AZ0bwkaLNgDaAXQCURNlBJbgUUjRqkQMB0+mH1e1JuMcr9x2dXEAA6TVpgFTUAGih5czxcQksra29bUgBJCiFpUkMABl9UtM1beWyIXIJ5Qt8UqwB6RqYAC0RNKAAbRFRoMBpkABNuOAhtBqhmyeaoACYAZgA2OcZMVuguiE5gAFpW0lQhqE1EIehkTjtBe0RgVqEIJDgoZEhHL2FuMriLq9BIFAAOSHEBA6IzFpA2QKJTg4HyRCUXrAIGvGjXKAQGFQAEQEKQyFQGhPbg0G6VIQhMgUah0KC7OKU6nkKi0EB+Eok4BkwRqZk0tn00idUHmRkqCo5Fm09l2UWoECmFIsZXsIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

type Action<T> = { category: T }


function myFunc<T = number>(actions: Action<T>[]) {
  const arrayArray = [actions]

  arrayArray.sort((a, b) => {
    const aItem = a[0]
    const bItem = b[0]

    // This line produces:
    //
    // 2362: The left-hand side of an arithmetic operation must be of type 'any',
    // 'number', 'bigint' or an enum type.
    //
    // return aItem.category - bItem.category

     return (aItem.category as any) - (bItem.category as any)
  })
}

ESLint Config

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

tsconfig

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

Expected Result

There should be no linting errors, since the as any type assertions are necessary.

Actual Result

The as any type assertions are reported as unnecessary.

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 5.33.1
@typescript-eslint/parser 5.33.1
TypeScript 4.7.4
ESLint 8.15.0
node web

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