Skip to content

Bug: [no-unnecessary-type-assertion] False report with an 'as' assertion #10257

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

Open
4 tasks done
FleetAdmiralJakob opened this issue Nov 1, 2024 · 7 comments · Fixed by #10523
Open
4 tasks done

Bug: [no-unnecessary-type-assertion] False report with an 'as' assertion #10257

FleetAdmiralJakob opened this issue Nov 1, 2024 · 7 comments · Fixed by #10523
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@FleetAdmiralJakob
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.6.2&fileType=.tsx&code=MYewdgzgLgBNCGUCmAuGAiJAPADgSwCckATdGAHwxyTGLzAHMyBeKmux9AbgCgeAzAK5hgUPOBgBbePQAUAShgBvHjBhEoggmGWq1MKAE9qaAAYASJQmQBfAEpIAjoKTRTMeBBgWlR6iH44KEQkeycXNz0bXhsgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0YANrhsOOdA7RImgLoaAviGNA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const state: "expired" | "pending" = "pending";

function main() {
  return {
    type: `${state}Request` as `${typeof state}Request`
  };
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

The code should be fine as the type assertion changes the type from string to "expiredRequest" | "pendingRequest"

Actual Result

There was an error 5th line saying:

This assertion is unnecessary since it does not change the type of the expression. 5:11 - 5:56

Additional Info

No response

@FleetAdmiralJakob FleetAdmiralJakob 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 Nov 1, 2024
@Josh-Cena
Copy link
Member

This is because as is a valid place for contextual type inference. Just change as to satisfies and you will preserve the function return type.

@FleetAdmiralJakob
Copy link
Author

But then it's not an unnecessary type assertion either. I mean you can use the type assertion satisfies is just better.

I mean the error is a little bit misleading as the quick fix suggests removing the type assertion not refactoring it to satisfies. In my case the type assertion / or the satisfies (either of them) is necessary, not optional.

Either the error should be removed or the quick fix updated I suggest.

@bradzacher
Copy link
Member

Alternatively you can use as const for this.

@abrahamguo
Copy link
Contributor

Here is a more minimal example:
let a = (Date.now() % 2 ? 'a' : 'b') as 'a' | 'b';

@kirkwaiblinger
Copy link
Member

Noting that this is closely related to #8721

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Dec 7, 2024
@JoshuaKGoldberg JoshuaKGoldberg changed the title Bug: [no-unnecessary-type-assertion] Does not work with literal types Bug: [no-unnecessary-type-assertion] False report with an 'as' assertion Dec 30, 2024
@JoshuaKGoldberg
Copy link
Member

Reopening since we reverted #10523.

@nicolo-ribaudo
Copy link

nicolo-ribaudo commented Jan 9, 2025

I have a similar bug, and I'm not sure if it's the same as this one or a different one because, differently from the one in this PR, changing as to satisfies as suggested above (#10257 (comment)) changes the types of the code

https://typescript-eslint.io/play/#ts=5.5.2&fileType=.tsx&code=GYVwdgxgLglg9mABMMAKA7gLkQIgIY6IA%2BuARjgJSIDeAUIohAgM5SIAOeUUApgE5IAvIgAGAEmroAviMR5moiVACe7HnGCJpIgNy16iPjyggBHLrwF6p%2BgPS3EASU3K4IQzwC2cAG49EUAAW-jjgYDwQPMzMeHzKctH8sAiEeKS%2BPAA0AYEwCvx8cHzMtAA2xh7M2PiEJDjkiMIoqDUUekA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RI4MAF8QOoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

function fn(w: "a" | "b") {
  const pattern = `${w}` as `${typeof w}`;

  return pattern;
}

// If you remove the "unnecessary assertion" above, this errors
let res: "a" | "b" = fn("a");

EDIT: This is exactly the same as the one in this issue, and replacing as with satisfies indeed works. I don't know what I tested 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants