Skip to content

no-conditional-empty-object-spread

Build conditional properties explicitly instead of spreading an empty-object branch.

Why

An empty-object conditional spread hides whether a property is omitted, which differs from assigning undefined.

Fix

Construct the object explicitly and add the conditional fields only when the original condition holds, preserving evaluation order and ownership.

Examples

Before — flagged Preserve the explicit contract
src/example.ts
declare const includePage: boolean;
const query = { limit: 20, ...(includePage ? { page: 1 } : {}) };
After — preferred Use the direct contract
src/example.ts
declare const includePage: boolean;
const query: { limit: number; page?: number } = { limit: 20 };
if (includePage) query.page = 1;