oxc-project/oxc · warning · OxcDiagnostic

Prefer consistent types when spreading a ternary in an array

Error message

Prefer consistent types when spreading a ternary in an array literal.

What it means

Diagnostic from oxlint's `unicorn/consistent-empty-array-spread` rule. When a ternary is spread into an array literal and one branch is an array while the other is the empty string `''`, the fallback type is inconsistent — both contribute nothing when spread, but mixing types is confusing. The rule suggests making both branches arrays. It is registered as a suggestion, so it is applied via the fix/suggestion flow rather than silently.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/consistent_empty_array_spread.rs:9

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{AstNode, ast_util::outermost_paren_parent, context::LintContext, rule::Rule};

fn consistent_empty_array_spread_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer consistent types when spreading a ternary in an array literal.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct ConsistentEmptyArraySpread;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// When spreading a ternary in an array, we can use both `[]` and `''` as fallbacks,
    /// but it's better to have consistent types in both branches.
    ///
    /// ### Why is this bad?
    ///
    /// Having consistent types in both branches makes the code easier to read and understand.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the empty-string fallback with `[]` so both branches are arrays.
  2. If strings are the intended element source, make both branches strings and spread outside the ternary.
  3. Prefer `.concat`/`.flat()` composition when branch types genuinely differ.

Example fix

// before
const parts = [
  a,
  ...(foo ? [b, c] : ''),
];
// after
const parts = [
  a,
  ...(foo ? [b, c] : []),
];
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: A ConditionalExpression directly inside a SpreadElement inside an ArrayExpression where exactly one branch is an ArrayExpression and the other is an empty string literal: `[...(foo ? [b, c] : '')]` or the mirrored `[...(foo ? 'bc' : [])]`. Ternaries in call arguments or elsewhere are not flagged.

Common situations: Building filter/part lists with conditional segments; code migrated from string-concatenation styles where `''` was the natural empty fallback; enabling the unicorn pedantic preset.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/25f954ac8b3773e1. Report an issue: GitHub.