An issue is raised when using a fallback empty object with the logical OR (||) or nullish coalescing (??) operators when
spreading values in object literals.
When spreading values in JavaScript object literals, falsy values like null, undefined, false,
0, or empty strings are safely ignored and don’t add any properties to the resulting object. This means that fallback patterns like
{…(foo || {})} or {…(foo ?? {})} are unnecessary.
The JavaScript spread operator in object literals has built-in safety for falsy values. When you write {…null} or
{…undefined}, no properties are added to the object, and no error is thrown. This makes defensive fallback objects redundant.
Using unnecessary fallbacks creates several problems:
The pattern likely comes from developers being overly cautious or from experience with array spreading, where […null] would indeed
throw an error. However, object spreading behaves differently and is more forgiving.
This issue primarily affects code maintainability and readability. While not a security or reliability concern, unnecessary fallback objects make the codebase more verbose and can mislead other developers about the actual behavior of the spread operator in object literals.
Remove the unnecessary fallback empty object when spreading in object literals. The spread operator safely handles falsy values without throwing errors.
const object = {...(foo || {})}; // Noncompliant
const object = {...foo};