8000 Bug: [no-unnecessary-type-assertion] false positive on type narrowing · Issue #11242 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [no-unnecessary-type-assertion] false positive on type narrowing #11242

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
cainlevy opened this issue May 22, 2025 · 2 comments
Closed
4 tasks done

Bug: [no-unnecessary-type-assertion] false positive on type narrowing #11242

cainlevy opened this issue May 22, 2025 · 2 comments
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@cainlevy
Copy link

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.3&fileType=.tsx&code=C4TwDgpgBAYgdlAvFA3gWAFBSgHgEpQQAewEcAJgM5SXABOAlnAOZQA%2BUArhRAGZMRyAPgAUASgBcUPAG5MAX0wBjAPZxaUYFPhIo4pEK49%2BcQXIyZe3JcAZqoAGxWUIAeToAZZxHFTajFnYoOE4AWwAjCDpUTGwGXj0AWQBDYAALADo6ZIoVUP0hZAAGDIBWMRisbCg6CGBOOgRgfWTqfyZmc2x5QgcXSuqauoam8S6oRQxJoA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg10NM8AOXapUAYQAW6aAGsylDul4BfECqA&tokens=false

Repro Code

type Fn = {
  <R extends string | undefined>(): R;
}
const t: Fn = () => undefined;

function loseOrLose(): string | number {
  if (Math.random() >= 0.5) {
    // no-unnecessary-type-assertion thinks the "as string" is unnecessary
    return t() as string;
  } else {
    // but without `as string` typescript fails
    return t();
  }
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true
  }
}

Expected Result

I expected that as string would be allowed, since TypeScript fails without it.

Actual Result

no-unnecessary-type-assertion claims the as string is unnecessary

Additional Info

This is similar to #11054, but I do not know if it has the same root cause.

@cainlevy cainlevy 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 May 22, 2025
@bradzacher
Copy link
Member

This is working as expected due to limitations in TS.

You have a generic that is only used in a return location. This causes TS to "backfill" the generic using the as's type. If you use intellisense you can see what I mean.

Because of this TS behaviour - when we inspect the types we see the signature of the function as () => string. So we error because based on the types you're doing string as string.

Of course if you remove the as then the function falls back to () => string | undefined - but we can't know that.

The fix is

a) don't use generics that are only used in return type locations. These generics are unsafe to begin with as they are impossible to implement correctly.
b) use the generic syntax instead t<string>()

@bradzacher bradzacher closed this as not planned Won't fix, can't repro, duplicate, stale May 22, 2025
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for team members to take a look labels May 22, 2025
@cainlevy
Copy link
Author

Thanks for the reply. It sounds like there's nothing ESLint can do about this one. The logic of no-unnecessary-type-assertion seems to have a number of false positive cases due to limitations in TS.

It also appears there is no effective workaround:

a) The function I'm using is from i18next, and is unfortunately not something I can refactor. I trust they have reasons for the result type as it exists and that the function implementation is more interesting than the simplistic return statement in my reproduction.
b) When I use the generic syntax t<string>() I get a no-unnecessary-type-arguments violation because string is the default value for the generic parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants
0