avoidThis conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present.small-rules/no-conditional-empty-object-spreadDisallow object spreads that conditionally spread an empty object to omit fields.
Ported from dmmulroy/anti-slop’s
no-conditional-empty-object-spread. The port exists for his ban on using {} as a no-op to hide omission.
Credit for the pattern is his.
Hidden intent: {...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, and the semantics differ.
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;avoidThis conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present.This rule does not accept options.
const obj = { ...value, ...(cond ? {} : other) };const obj = { ...value, ...(cond ? other : another) };