-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
docs: [no-wrapper-object-types] clean up a bit of phrasing #9363
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
docs: [no-wrapper-object-types] clean up a bit of phrasing #9363
Conversation
Thanks for the PR, @kirkwaiblinger! 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. |
@@ -10,13 +10,12 @@ import TabItem from '@theme/TabItem'; | |||
> See **https://typescript-eslint.io/rules/no-wrapper-object-types** for documentation. | |||
|
|||
The JavaScript language has a set of language types, but some of them correspond to two TypeScript types, which look similar: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, `object`/`Object`. | |||
The difference is that the lowercase variants are compiler intrinsics and specify the actual _runtime types_ (that is, the return value when you use the `typeof` operator), while the uppercase variants are _structural types_ defined in the library that can be satisfied by any user-defined object with the right properties, not just the real primitives. | |||
The difference is that the lowercase variants are compiler intrinsics and specify the actual _runtime types_ (that is, the type indicated when executing `typeof x` at runtime), while the uppercase variants are _structural types_ defined in the library that can be satisfied by any user-defined object with the right properties, including but not limited to the real primitives. |
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.
made slight edit here to try to clarify that it's about the runtime typeof
operator in TS vs the type-time typeof
operator, which also exists.
☁️ Nx Cloud ReportCI is running/has finished running commands for commit f7d7253. 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 1 targetSent 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.
🙌 lovely!
It is widely considered a JavaScript best practice to work directly with the built-in primitives, like `0`, rather than objects that "look like" numbers, like `new Number(0)`. | ||
Primitives are simpler to conceptualize, work with `==` and `===` equality checks -- which their object equivalents do notDeepEqual -- and have well-known behavior around truthiness/falsiness which is common to rely on. |
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.
It is widely considered a JavaScript best practice to work directly with the built-in primitives, like `0`, rather than objects that "look like" numbers, like `new Number(0)`. | |
Primitives are simpler to conceptualize, work with `==` and `===` equality checks -- which their object equivalents do notDeepEqual -- and have well-known behavior around truthiness/falsiness which is common to rely on. | |
It is widely considered a JavaScript best practice to work directly with the built-in primitives, like `0`, rather than objects that "look like" numbers, like `new Number(0)`. | |
Primitives work with `==` and `===` equality checks and have more commonly relied-on behavior around truthiness/falsiness, while two objects that wrap the same primitive values do not compare equal, and _all_ wrapper objects are truthy regardless of the wrapped value. | |
Furthermore TypeScript prevents using `Number` in places that expect `number`—if a function accepts `number`, you cannot pass a value of type `Number` to it. | |
You also cannot do subtraction on `Number` values. |
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'll think about this a bit. Two things I wanna pay attention to
number
is the specific example in this section, but it's supposed to make a point about the primitives in general, not just numbers- If we give too much justification, it can seem like this is something controversial that we're just weirdly strongly opinionated about, rather than something that's not even slightly in dispute by anyone. 😆
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.
That's true. Well it's up to your decision. I certainly want to keep this phrasing though:
Primitives work with
==
and===
equality checks and have more commonly relied-on behavior around truthiness/falsiness, while two objects that wrap the same primitive values do not compare equal, and all wrapper objects are truthy regardless of the wrapped value.
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.
Adapted this a bit to make space for examples, each using a different primitive type. Let me know what you think!
@@ -10,13 +10,16 @@ import TabItem from '@theme/TabItem'; | |||
> See **https://typescript-eslint.io/rules/no-wrapper-object-types** for documentation. | |||
|
|||
The JavaScript language has a set of language types, but some of them correspond to two TypeScript types, which look similar: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, `object`/`Object`. |
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.
While we're here 😄 I'm re-reading this and finding it hard to parse... maybe:
The JavaScript language has a set of language types, but some of them correspond to two TypeScript types, which look similar: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, `object`/`Object`. | |
The JavaScript language has a set of built-in ("intrinsic") type pairs which look similar: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, and `object`/`Object`. |
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## v8 #9363 +/- ##
==========================================
- Coverage 88.37% 87.42% -0.95%
==========================================
Files 419 390 -29
Lines 14610 13245 -1365
Branches 4274 3833 -441
==========================================
- Hits 12911 11580 -1331
+ Misses 1375 1363 -12
+ Partials 324 302 -22
Flags with carried forward coverage won't be shown. Click here to find out more. |
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.
Yay!
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 have some reservations but I think this generally looks great
JavaScript programs typically work with the real number primitives, rather than objects that "look like" numbers. | ||
Primitives are simpler to conceptualize and work with `==` and `===` equality checks -- which their object equivalents do notDeepEqual. | ||
As a result, using the lowercase type names like `number` instead of the uppercase names like `Number` helps make your code behave more reliably. | ||
The JavaScript language has a set of built-in ("intrinsic") types which look similar: `boolean`/`Boolean`, `number`/`Number`, `string`/`String`, `bigint`/`BigInt`, `symbol`/`Symbol`, and `object`/`Object`. |
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 this loses the nuance that we tried hard to keep in the original docs. JavaScript only has eight types: number
, object
, string
, symbol
, boolean
, bigint
, undefined
, null
. Boolean
, Number
, etc. are subtypes of object
. It's only in TypeScript-land where they are sort of parallel and can be compared to each other.
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 actually agree. I would like to go with something more similar to the original wording as well.
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.
Neither here nor there, but
Boolean
,Number
, etc. are subtypes ofobject
.
TIL that
let o: object = false; // <-- TS error
// BUT
let b: Boolean = false;
o = b; // no error lol
The wrapper types are so bad lol
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.
Yep—everything that doesn't claim to be a primitive is implicitly object
, including {}
. object
does not actually mean "non-primitive"; it just means "is not definitely primitive".
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.
Looks great to me! Just the one nit, not a blocker if you disagree. Thanks! 🎉
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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 this!
PR Checklist
fixes #000followup to https://github.com/typescript-eslint/typescript-eslint/pull/9102/files/f25f33396926cf0bb03eac1d02d8636987d3ec28..e078105858728c2cfe6bdf81a145feae291cb43c#r1635229512Overview