Skip to content

No Conditional Empty Object Spread

Suggestion
small-rules/no-conditional-empty-object-spread

Disallow object spreads that conditionally spread an empty object to omit fields.

Rationale

Ported from dmmulroy/anti-slop’s no-conditional-empty-object-spread — I wanted his ban on using {} as a no-op to hide omission. Credit for the pattern is his.

  • Intent is hidden: ...(cond ? {} : { prop: value }) reads as a merge when you mean an optional field. You have to puzzle out which branch is the empty one.

  • Clarity over brevity: building the object and assigning the field only when present keeps the type as a plain optional property and avoids a throwaway allocation. Probably not a perf win you’ll measure, but it’s the more direct spelling.

  • Scope matters: the rule only flags object spreads. Array spreads like [...(cond ? {} : other)] are fine — different semantics.

I’d rather you write it as two statements — you probably won’t need the ternary at all:

const obj: { prop?: string } = { ...base };
if (condition) obj.prop = value;

Diagnostic Messages

avoid
This conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/no-conditional-empty-object-spread": "error"
}
}

Examples

conditional empty object spread
const obj = { ...value, ...(cond ? {} : other) };
no empty object in either branch
const obj = { ...value, ...(cond ? other : another) };