-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): use consistent naming for asserting types and casting values #10472
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
fix(eslint-plugin): use consistent naming for asserting types and casting values #10472
Conversation
Thanks for the PR, @ronami! 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 on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
☁️ Nx Cloud ReportCI is running/has finished running commands for commit f003891. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 2 targetsSent with 💌 from NxCloud. |
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.
Noting that there are a fair number of problematic usages of "cast" remaining that we'll want to address still, either here or in a followup
@@ -176,7 +176,7 @@ Also, if you would like to ignore all primitives types, you can set `ignorePrimi | |||
|
|||
{/* insert option description */} | |||
|
|||
Whether to ignore expressions that coerce a value into a boolean: `Boolean(...)`. | |||
Whether to ignore expressions that cast a value into a boolean: `Boolean(...)`. |
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.
Let's leave this this as "coerce".
I've just realized my VSCode had match-case and match-whole-word locked in (🙈). I think I've managed to update every usage of Thanks! |
packages/eslint-plugin/docs/rules/explicit-function-return-type.mdx
Outdated
Show resolved
Hide resolved
Looks like there's still two more places where "cast" is used in user-facing strings...
typescript-eslint/packages/eslint-plugin/src/rules/strict-boolean-expressions.ts Line 103 in d09d36d
... and then also here, though I'm honestly not sure why we even have this: typescript-eslint/packages/eslint-plugin/docs/rules/strict-boolean-expressions.mdx Lines 171 to 201 in d09d36d
|
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.
couple minor comments
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #10472 +/- ##
==========================================
+ Coverage 86.76% 86.78% +0.02%
==========================================
Files 444 445 +1
Lines 15311 15365 +54
Branches 4457 4475 +18
==========================================
+ Hits 13285 13335 +50
- Misses 1672 1675 +3
- Partials 354 355 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
I've noticed these, too, but to my understanding, the meaning in those cases is actually to cast rather than to assert. They all require the developer to make a runtime change rather than a type-only assertion. For example, these two reports (1, 2) include a suggestion to "cast" a value to a boolean with I think these are similar to the coerce meaning in #10472 (comment), and also to the meaning of casting/coercing in no-extra-boolean-cast. I'm not sure which terminology to use in those cases. There are some places we use the term coercing, some casting, and here (open PR) we use converting. |
Gotcha, yeah, I see where you're coming from. My spiel on this is: within a pure JS context, one can (just barely) get away with using "cast" as in no-extra-boolean-cast. IMO it's still not a good term for what it's describing, but it is at least unambiguous, since "cast" has no other possible meaning within JS. But, throw TS into the mix, and "cast" means different things to different people, and even sometimes different things to the same people in different contexts 😆. Therefore, I don't think we should use it for either the "type assertion" or "type conversion/coersion" meanings at all in a TS context. So, then, to your specific question, I think we should change those terms in SBE, and we should change them to "convert"/"conversion". My vague idea of "coercion" vs "conversion" is that "coercion" is what happens when the language performs a type conversion implicitly, usually because objects of incompatible types are interacting, like Incidentally, regarding #10472 (comment), that does imply that the word "convert" would be most in line with my previous paragraph. But, the option name is |
Yes! I can't agree more about how confusing "to cast" is, especially with TS, as it can have so many meanings. I've updated the PR and adjusted the "to cast" usage in SBE, too; thanks! |
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.
Love it! Thank you for doing this!! This had been bugging me for a while 😆
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.
Lovely!
##### [v8.18.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8181-2024-12-16) ##### 🩹 Fixes - **scope-manager:** visit params decorator before nest scope ([#10475](typescript-eslint/typescript-eslint#10475)) - **eslint-plugin:** \[no-unnecessary-condition] better message when comparing between literal types ([#10454](typescript-eslint/typescript-eslint#10454)) - **eslint-plugin:** use consistent naming for asserting types and casting values ([#10472](typescript-eslint/typescript-eslint#10472)) - **eslint-plugin:** \[no-unnecessary-boolean-literal-compare] flag values of a type parameter with boolean type constraints ([#10474](typescript-eslint/typescript-eslint#10474)) - **eslint-plugin:** handle string like index type ([#10460](typescript-eslint/typescript-eslint#10460)) - **eslint-plugin:** \[no-unnecessary-template-expression] don't report when an expression includes comment ([#10444](typescript-eslint/typescript-eslint#10444)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) 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.18.0 | 8.18.1 | | npm | @typescript-eslint/parser | 8.18.0 | 8.18.1 | ## [v8.18.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8181-2024-12-16) ##### 🩹 Fixes - **scope-manager:** visit params decorator before nest scope ([#10475](typescript-eslint/typescript-eslint#10475)) - **eslint-plugin:** \[no-unnecessary-condition] better message when comparing between literal types ([#10454](typescript-eslint/typescript-eslint#10454)) - **eslint-plugin:** use consistent naming for asserting types and casting values ([#10472](typescript-eslint/typescript-eslint#10472)) - **eslint-plugin:** \[no-unnecessary-boolean-literal-compare] flag values of a type parameter with boolean type constraints ([#10474](typescript-eslint/typescript-eslint#10474)) - **eslint-plugin:** handle string like index type ([#10460](typescript-eslint/typescript-eslint#10460)) - **eslint-plugin:** \[no-unnecessary-template-expression] don't report when an expression includes comment ([#10444](typescript-eslint/typescript-eslint#10444)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) - Ronen Amiel - YeonJuan [@yeonjuan](https://github.com/yeonjuan) 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.
PR Checklist
Overview
This PR addresses #10458 and makes consistent use of asserting vs casting.
There is some inconsistency for casting values: to cast (
no-extra-boolean-cast
), to coerce (e.g.no-implicit-coercion
, MDN), to convert (upcomingno-unnecessary-type-conversion
). To my understanding, these all mean the same thing.I've used consistent language for casting a value (currently using
to cast
). Please let me know if this sounds OK or if I should change it back.