This rule raises an issue when a ternary expression used with the spread operator in an array literal has branches with different types (array vs string).

Why is this an issue?

When spreading a ternary expression in an array literal, using different types in the branches can make code harder to understand and maintain.

In JavaScript, both arrays and strings are iterable, so they can both be spread into arrays. However, mixing types creates inconsistency that can confuse developers reading the code.

For example, …​(condition ? [1, 2] : '') works because an empty string spreads to nothing, but it’s not immediately clear to readers that this is the intended behavior. Using …​(condition ? [1, 2] : []) makes the intent much clearer.

Consistent typing also makes the code more predictable. If you later need to modify one branch, you won’t accidentally introduce type-related bugs by forgetting that the other branch expects a different type.

What is the potential impact?

This issue primarily affects code readability and maintainability. While it doesn’t cause runtime errors, it can lead to confusion during code reviews and make future modifications more error-prone.

How to fix?

Use consistent types in both branches of the ternary. If one branch returns an array, make sure the other branch also returns an array (use an empty array [] instead of an empty string).

Non-compliant code example

const array = [
  a,
  ...(foo ? [b, c] : ''), // Noncompliant
];

Compliant code example

const array = [
  a,
  ...(foo ? [b, c] : []),
];

Documentation