Skip to content

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

Conversation

kirkwaiblinger
Copy link
Member

PR Checklist

Overview

@typescript-eslint
Copy link
Contributor

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.

Copy link

netlify bot commented Jun 15, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit f7d7253
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/668b83dd41b8080008afb753
😎 Deploy Preview https://deploy-preview-9363--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 5 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

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.
Copy link
Member Author

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.

Copy link

nx-cloud bot commented Jun 15, 2024

☁️ Nx Cloud Report

CI 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 target

Sent with 💌 from NxCloud.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌 lovely!

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Jun 16, 2024
Comment on lines 17 to 18
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.
Copy link
Member

@Josh-Cena Josh-Cena Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Member Author

@kirkwaiblinger kirkwaiblinger Jun 16, 2024

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

  1. number is the specific example in this section, but it's supposed to make a point about the primitives in general, not just numbers
  2. 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. 😆

Copy link
Member

@Josh-Cena Josh-Cena Jun 16, 2024

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.

Copy link
Member Author

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`.
Copy link
Member

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:

Suggested change
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`.

Copy link

codecov bot commented Jun 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.42%. Comparing base (d685948) to head (3821222).
Report is 94 commits behind head on v8.

Current head 3821222 differs from pull request most recent head f7d7253

Please upload reports for the commit f7d7253 to get more accurate results.

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     
Flag Coverage Δ
unittest 87.42% <ø> (-0.95%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 52 files with indirect coverage changes

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay!

Josh-Cena
Josh-Cena previously approved these changes Jun 27, 2024
Copy link
Member

@Josh-Cena Josh-Cena left a 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`.
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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 of object.

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

Copy link
Member

@Josh-Cena Josh-Cena Jul 4, 2024

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".

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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>
Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

@kirkwaiblinger kirkwaiblinger merged commit 46dd42f into typescript-eslint:v8 Jul 12, 2024
32 of 61 checks passed
@kirkwaiblinger kirkwaiblinger deleted the kirk-no-wrapper-object-types-docs branch July 12, 2024 06:12
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants