Rule proposal: Forbid `...(foo || {})` and `...(foo ?? {})` in object literal
See original GitHub issueUnlike spread in array literals, spread falsy values in object won’t throw error, and works as expected. There is no need to add empty object {}
as fallback.
Fail
const object = {
foo,
...(bar || {}),
};
const object = {
foo,
...(bar ?? {}),
};
Pass
const object = {
foo,
...bar,
};
const array = [
foo,
...(bar || []),
];
Not sure about the rule name, prefer-directly-spread
? no-useless-empty-object
?
Spread falsy value
[
0,
false,
undefined,
'',
NaN,
0n,
null,
document.all, // The only exception, but who care?
].map(value => Object.keys(({...value})).length)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:7 (1 by maintainers)
Top Results From Across the Web
How to rewrite code to avoid TSLint "object access via string ...
You have a couple options here: 1) Just disable the rule /* tslint:disable:no-string-literal */ whatever.
Read more >Airbnb JavaScript Style Guide()
1.2 Complex: When you access a complex type you work on a reference to its value. object; array; function. const foo = [ ......
Read more >Google JavaScript Style Guide
1 Introduction. This document serves as the complete definition of Google's coding standards for source code in the JavaScript programming language.
Read more >typescript get all properties of object - Lena's Italian Restaurant
Say you have simple object like the following: 1 2 3 4 const obj = { foo: ... if we use object literals...
Read more >prefer-optional-chain | typescript-eslint
optional chain expressions provide undefined if an object is null or undefined ... this rule also supports converting chained strict nullish checks: foo...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
no-useless-fallback-in-spread
?Thank you @papb, that makes sense 🙂 However, I’m sure I noticed it from a real world example of my issue. I’ll turn the rule back on in my shared eslint config and see if I can find the project it affected. If not, then happy days!