Skip to content

Bug: [no-unnecessary-type-assertion] false positive for string literals #10723

Closed as duplicate of#10257
@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=MYewdgzgLgBAtgTwHIFc4CMCmAnGBeGAJgFYBYAKFEljgEMAPfGATgAYKKroYAHHYTGFgEABgBIA3gAoAsrSgALAHRwAlmCmJUGHABp4DAJSGYAehgBGVqwC%2BAUhExaEGOIlg0WbPZEBuIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QeoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const myNumber = 25
const max = 90

const percent = `${(Math.min(myNumber, max)) / 100}%` as `${number}%`;

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

This assertion is unnecessary since it does not change the type of the expression. 4:17 - 4:70

Additional Info

Without the type assertion, percent would just be inferred as string, but we want it to be ${number}%.

While it’s true that as const would give us the correct inference here, as ${number}% should be allowed, too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglocked 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-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions