Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
TkDodo opened this issue Jan 28, 2025 · 2 comments
Closed
4 tasks done

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

TkDodo opened this issue Jan 28, 2025 · 2 comments
Labels
bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look

Comments

@TkDodo
Copy link

TkDodo commented Jan 28, 2025

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.

@TkDodo TkDodo added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jan 28, 2025
@ronami
Copy link
Member

ronami commented Jan 28, 2025

That's an interesting issue (I think it's definitely a bug), though I think it's a duplicate of #10257.

To my understanding, the type doesn't qualify as a literal and gets checked as a non-literal, even though the rule has special logic for template literals. If this used as const instead, I think the rule will treat it as a literal (as the asserted type is checked for being a literal) and this should pass (playground link).

I've written some thoughts about this (along with tackling this particular case) in #10631.

@JoshuaKGoldberg
Copy link
Member

Agreed, this is a valid bug and also a subset of #10257. Fun!

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Feb 25, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look
Projects
None yet
Development

No branches or pull requests

3 participants