consistent-conditional-object-spread

June 23, 2026 Β· View on GitHub

πŸ“ Enforce consistent conditional object spread style.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

When conditionally spreading properties into an object literal, pick one style consistently.

By default, this rule prefers && because it avoids an unnecessary empty object branch.

Examples

// ❌
const object = {...(condition ? {property} : {})};

// βœ…
const object = {...(condition && {property})};

An undefined or null fallback branch spreads nothing, so it is treated the same as an empty object:

// ❌
const object = {...(condition ? {property} : undefined)};

// βœ…
const object = {...(condition && {property})};

With the 'ternary' option:

// eslint unicorn/consistent-conditional-object-spread: ["error", "ternary"]

// ❌
const object = {...(condition && {property})};

// βœ…
const object = {...(condition ? {property} : {})};

Options

Type: string
Default: 'logical'

Available options:

  • 'logical' - Prefer ...(condition && object).
  • 'ternary' - Prefer ...(condition ? object : {}).