-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [ban-types] Suggest using object
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
People vexed by ```ts declare let j: unknown; let m = { ...(j as {}) }; ``` being banned by `ban-types`'s default of disallowing `{}` have resorted to using ```ts let 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 anything let m2 = { ...(j as unknown) }; // Adds undesirable index signature to result type let m3 = { ...(j as Record<string, never>) }; // Adds undesirable index signature to result type let m4 = { ...(j as Record<string, unknown>) }; ``` The "correct" fix is ```ts let 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 effects
👷 Deploy request for typescript-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 on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
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.
|
The issue with Once you've got an function foo(arg: object) {
arg.foo; // can't access random properties
if ('foo' in arg) {
arg.foo; // can't do guarded random property access either
}
} So as a "type meaning "any object"" replacement - OTOH function foo(arg: Record<string, unknown>) {
arg.foo; // can access random properties
if ('foo' in arg) {
arg.foo; // can do guarded random property access either
}
} Really once you've got an |
I don't feel like you addressed the scenario (spread) covered in the OP. All of the workarounds make the resultant type less safe by adding an index signature to |
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! |
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 |
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
object
has been shipping in TS for several years, this seems like a safe suggestion in lieu ofRecord
's messy side effectsPR Checklist
Overview