Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-unnecessary-type-conversion] add rule#10182
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for the PR,@skondrashov! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently onhttps://opencollective.com/typescript-eslint. |
netlifybot commentedOct 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
✅ Deploy Preview fortypescript-eslint ready!
To edit notification comments on pull requests, go to yourNetlify site configuration. |
nx-cloudbot commentedOct 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
View yourCI Pipeline Execution ↗ for commit6822a69.
☁️Nx Cloud last updated this comment at |
skondrashov commentedOct 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Sorry for force-push, wasn't sure how else to resolve the merge conflicts other than redoing the commit cause they didn't show up locally. Doesn't seem like it did anything to the merge conflict though, so I'm out of ideas. I see I have problems from the perfectionist plugin in my files as well, but the plugin doesn't run locally when I use Would be happy to address the 2 missing lines of coverage at some point if everything else gets figured out. |
codecovbot commentedOct 20, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@## main #10182 +/- ##==========================================+ Coverage 90.84% 90.90% +0.06%========================================== Files 497 498 +1 Lines 50320 50655 +335 Branches 8311 8335 +24 ==========================================+ Hits 45714 46049 +335 Misses 4591 4591 Partials 15 15
Flags with carried forward coverage won't be shown.Click here to find out more.
🚀 New features to boost your workflow:
|
kirkwaiblinger commentedOct 21, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
(hint) - We often use // setupdeclareletstr:string|number;declareconstnum:number|string;// etc It's a useful pattern for testing sometimes too, since you may run into surprising behavior when
Makes sense 👍
Yep, also makes sense. And, even if TS did allow them, we don't need to stress about identifying every possible way for a value to be coerced, just the common patterns, and others can always be added in the future, too. Appreciate your close attention to detail on all these, though!
This is an astute observation. Is there anything this rule would flag that that rule doesn't flag? If not, I'd lean towards letting that separate rule handle this case. If you want you could even put a note in the docs that tells the user that template expressions intentionally aren't handled because they're already dealt with more thoroughly in the
Hmm, I hear you. I personally think either one is fine. Maybe someone else will care more strongly 🤷
I think for these reports, reporting the whole node is totally fine declareconsts:string;s+'';~~~~~~!!(longExpressionThatsABoolean);~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ But, I really like what you've done with the more granular approach. !!(longExpressionThatsABoolean);~~ So I'd say go for it! But if it becomes an implementation hassle, no stress either if you end up falling back to the typical strategy of letting the |
No worries! The real pain is just force pushing between reviews, not before a reviewer has looked at it at all 🙂.
Oh, so I think the trouble that you're having is that you've used the
But you're in a slightly awkward scenario since your branch is on main. What I personally do (which differs from the above) is to set a second remote on your local repo, i.e. LMK if this helps or if you'd like some more help. (I can also push things directly to your branch to bail you out if needed 🤣). And no stress if you end up doing a force push again this time to get yourself in a clean state here! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is great work! And especially impressive considering it's your first PR to typescript-eslint. Well done!
Requesting a few changes, but nothing major.
🙂
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/tests/rules/no-unnecessary-coercion.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/tests/rules/no-unnecessary-coercion.test.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Agreed; let's go with |
…th more than just string types
👋 Just checking in@skondrashov - is this still something you have time and energy for? |
I'll have time to finish up the changes probably early in the new year! |
Awesome! I'll move this to draft then in the meantime, so we don't bug you. 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think I'm happy with this! Thanks for your very detailed work on this. Outstanding first PR!
packages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.mdx OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
I amsalivating at how many existing violations were removed in this PR. 🤤 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Awesome, this is looking very good! A stellar first PR indeed 🏆. Thanks for pushing on this!
Just a few small things from me - nothing we can't just do before merge if you don't have the time.
packages/eslint-plugin/docs/rules/no-unnecessary-type-conversion.mdx OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
packages/eslint-plugin/src/rules/no-unnecessary-type-conversion.ts OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
@@ -4,7 +4,6 @@ import rule from '../../src/rules/await-thenable'; | |||
import { getFixturesRootDir } from '../RuleTester'; | |||
const rootDir = getFixturesRootDir(); | |||
const messageId = 'await'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Nit: it would be nice to have these split out into their own PR. I'm not going to block on this though.
6822a69
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
ccbfcdc
intotypescript-eslint:mainUh oh!
There was an error while loading.Please reload this page.
| datasource | package | from | to || ---------- | -------------------------------- | ------ | ------ || npm | @typescript-eslint/eslint-plugin | 8.31.0 | 8.32.0 || npm | @typescript-eslint/parser | 8.31.0 | 8.32.0 |## [v8.32.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8320-2025-05-05)##### 🚀 Features- **eslint-plugin:** \[only-throw-error] add option `allowRethrowing` ([#11075](typescript-eslint/typescript-eslint#11075))- **eslint-plugin:** \[no-unnecessary-type-conversion] add rule ([#10182](typescript-eslint/typescript-eslint#10182))##### 🩹 Fixes- **eslint-plugin:** \[prefer-nullish-coalescing] fix parenthesization bug in suggestion ([#11098](typescript-eslint/typescript-eslint#11098))- **eslint-plugin:** \[unified-signatures] exempt `this` from optional parameter overload check ([#11005](typescript-eslint/typescript-eslint#11005))- **eslint-plugin:** \[no-unnecessary-type-parameters] should parenthesize type in suggestion fixer if necessary ([#10907](typescript-eslint/typescript-eslint#10907))##### ❤️ Thank You- Andy Edwards- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)- mdm317- Sasha Kondrashov- Yukihiro Hasegawa [@y-hsgw](https://github.com/y-hsgw)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.31.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8311-2025-04-28)##### 🩹 Fixes- **eslint-plugin:** \[no-unnecessary-condition] downgrade fix to suggestion ([#11081](typescript-eslint/typescript-eslint#11081))##### ❤️ Thank You- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to || ---------- | -------------------------------- | ------ | ------ || npm | @typescript-eslint/eslint-plugin | 8.31.0 | 8.32.0 || npm | @typescript-eslint/parser | 8.31.0 | 8.32.0 |## [v8.32.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8320-2025-05-05)##### 🚀 Features- **eslint-plugin:** \[only-throw-error] add option `allowRethrowing` ([#11075](typescript-eslint/typescript-eslint#11075))- **eslint-plugin:** \[no-unnecessary-type-conversion] add rule ([#10182](typescript-eslint/typescript-eslint#10182))##### 🩹 Fixes- **eslint-plugin:** \[prefer-nullish-coalescing] fix parenthesization bug in suggestion ([#11098](typescript-eslint/typescript-eslint#11098))- **eslint-plugin:** \[unified-signatures] exempt `this` from optional parameter overload check ([#11005](typescript-eslint/typescript-eslint#11005))- **eslint-plugin:** \[no-unnecessary-type-parameters] should parenthesize type in suggestion fixer if necessary ([#10907](typescript-eslint/typescript-eslint#10907))##### ❤️ Thank You- Andy Edwards- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)- mdm317- Sasha Kondrashov- Yukihiro Hasegawa [@y-hsgw](https://github.com/y-hsgw)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.31.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8311-2025-04-28)##### 🩹 Fixes- **eslint-plugin:** \[no-unnecessary-condition] downgrade fix to suggestion ([#11081](typescript-eslint/typescript-eslint#11081))##### ❤️ Thank You- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to || ---------- | -------------------------------- | ------ | ------ || npm | @typescript-eslint/eslint-plugin | 8.31.0 | 8.32.0 || npm | @typescript-eslint/parser | 8.31.0 | 8.32.0 |## [v8.32.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8320-2025-05-05)##### 🚀 Features- **eslint-plugin:** \[only-throw-error] add option `allowRethrowing` ([#11075](typescript-eslint/typescript-eslint#11075))- **eslint-plugin:** \[no-unnecessary-type-conversion] add rule ([#10182](typescript-eslint/typescript-eslint#10182))##### 🩹 Fixes- **eslint-plugin:** \[prefer-nullish-coalescing] fix parenthesization bug in suggestion ([#11098](typescript-eslint/typescript-eslint#11098))- **eslint-plugin:** \[unified-signatures] exempt `this` from optional parameter overload check ([#11005](typescript-eslint/typescript-eslint#11005))- **eslint-plugin:** \[no-unnecessary-type-parameters] should parenthesize type in suggestion fixer if necessary ([#10907](typescript-eslint/typescript-eslint#10907))##### ❤️ Thank You- Andy Edwards- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)- mdm317- Sasha Kondrashov- Yukihiro Hasegawa [@y-hsgw](https://github.com/y-hsgw)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.## [v8.31.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8311-2025-04-28)##### 🩹 Fixes- **eslint-plugin:** \[no-unnecessary-condition] downgrade fix to suggestion ([#11081](typescript-eslint/typescript-eslint#11081))##### ❤️ Thank You- Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger)You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
Uh oh!
There was an error while loading.Please reload this page.
PR Checklist
Overview
Shows autofixable errors for:
with the following error messaging:
Does not (and should not) show errors for:
Does not do anything with Symbol() because everything weird I tried to do with symbols was already a typescript error.
Does not do anything for some conversions to number which are already forbidden by typescript ("The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type."), including:
Does not cover
${"123"}
, because that overlaps with theno-unnecessary-template-expression
rule. Need feedback on what to do about that.I went with the word "coercion" because it was chosen by@Josh-Cena in the original issue but I think it's a poor choice. I think "conversion" or even "type conversion idiom" might be the most precise, because
.toString()
for example doesn't rely on coercion, and while!!value
and!value
both coerce the type ofvalue
to a boolean, it's the property of the idiom!!
that it can be used for type conversion which puts it under the umbrella of this rule. So I thinkno-unnecessary-type-conversion
is a better name for this rule, given the things that I'm currently having it check for.Catches the following problems in this repository (separate commit):
Might not have the best placement for the error scope, and might not have the best formatting for autofixes, could use feedback on those too.