Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [ban-types] Suggest usingobject to mean "any object"#5018
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
People vexed by```tsdeclare let j: unknown;let m = { ...(j as {}) };```being banned by `ban-types`'s default of disallowing `{}` have resorted to using```tslet m = { ...(j as never) };```which TS 4.7 flags as an error, for good reason. The alternatives suggested by this rule are all inappropriate for the situation:```ts// Doesn't fix anythinglet m2 = { ...(j as unknown) };// Adds undesirable index signature to result typelet m3 = { ...(j as Record<string, never>) };// Adds undesirable index signature to result typelet m4 = { ...(j as Record<string, unknown>) };```The "correct" fix is```tslet m = { ...(j as object) };```Now that `object` has been shipping in TS for several years, this seems like a safe suggestion in lieu of `Record`'s messy side effectsnx-cloudbot commentedMay 19, 2022 • 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 request fortypescript-eslint pending review.Visit the deploys page to approve it
|
Thanks for the PR,@RyanCavanaugh! 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. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitorsper day. |
codecovbot commentedMay 19, 2022 • 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 Report
@@ Coverage Diff @@## main #5018 +/- ##==========================================+ Coverage 91.32% 93.79% +2.46%========================================== Files 132 286 +154 Lines 1487 9795 +8308 Branches 224 2930 +2706 ==========================================+ Hits 1358 9187 +7829- Misses 65 328 +263- Partials 64 280 +216
Flags with carried forward coverage won't be shown.Click here to find out more.
|
bradzacher commentedMay 19, 2022 • 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.
The issue with Once you've got an functionfoo(arg:object){arg.foo;// can't access random propertiesif('foo'inarg){arg.foo;// can't do guarded random property access either}} So as a "type meaning "any object"" replacement - OTOH functionfoo(arg:Record<string,unknown>){arg.foo;// can access random propertiesif('foo'inarg){arg.foo;// can do guarded random property access either}} Really once you've got an |
RyanCavanaugh commentedMay 19, 2022 • 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.
I don't feel like you addressed the scenario (spread) covered in the OP. All of the workarounds make the resultant typeless safe by adding an index signature to |
bradzacher commentedMay 20, 2022
Is it common that people want to spread an Definitely correct that it's technically a safety hole in of itself. I'd be more than happy if you want to specifically mention the spread case in the error message to help inform people of the right way to do things! |
RyanCavanaugh commentedMay 20, 2022 • 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.
The reason I bring this up is we found some new breaks (e.g.https://github.com/facebook/docusaurus/blob/c16a08cba59bea5e48d3a79cee82c23aaae29876/packages/docusaurus-theme-classic/src/theme/CodeBlock/Container/index.tsx#L26 ) due to TS 4.7 treating a spread of If people want an object, meaning something whose proto is at least Sort of an aside and I'll likely be opening a new issue: There's just a lot of confusion likely to come down the road on this specific ban in general, since we're likely to change the definition of The rest of |
Josh-Cena commentedMay 20, 2022
FWIW, I'm ambivalent on banning |
People vexed by
being banned by
ban-types's default of disallowing{}have resorted to usingwhich TS 4.7 flags as an error, for good reason. The alternatives suggested by this rule are all inappropriate for the situation:
The "correct" fix is
Now that
objecthas been shipping in TS for several years, this seems like a safe suggestion in lieu ofRecord's messy side effectsPR Checklist
Overview