swc-project/swc · error · TypeError

Invalid attempt to destructure non-iterable instance. In ord

Error message

Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.

What it means

SWC's ES2015 destructuring transform lowers `[a, ...rest] = value` into a call chain ending in `_sliced_to_array`/`_to_array`. If the value is neither an iterable (`Symbol.iterator`) nor array-like, every conversion helper declines and the final fallback `_non_iterable_rest()` throws this TypeError. This mirrors the native engine behavior for destructuring a non-iterable with a rest element.

Source

Thrown at crates/swc_ecma_transforms_base/src/helpers/generated/_non_iterable_rest.rs:11

// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.

use super::{HelperDef, HelperName};

pub const DEF: HelperDef = HelperDef {
    name: HelperName::non_iterable_rest,
    local: "_non_iterable_rest",
    import_path: "@swc/helpers/_/_non_iterable_rest",
    #[cfg(feature = "inline-helpers")]
    source: r#"function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
"#,
    #[cfg(feature = "inline-helpers")]
    deps: super::HelperBitmap::from_bits(0x00000000000000040000000000000000),
};

#[cfg(feature = "inline-helpers")]
pub fn stmts() -> &'static [swc_ecma_ast::Stmt] {
    static STMTS: once_cell::sync::Lazy<Vec<swc_ecma_ast::Stmt>> =
        once_cell::sync::Lazy::new(|| super::super::parse(DEF.source, DEF.import_path));
    &STMTS
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Validate the value is iterable before destructuring: `if (v != null && typeof v[Symbol.iterator] === 'function')`.
  2. If the value may be array-like only, normalize first with `Array.from(v)`.
  3. Fix the data source so the destructuring site actually receives an array (e.g. destructure `resp.items`, not `resp`).
  4. Provide a safe default at the boundary: `const [first, ...rest] = expectedArray ?? [];`

Example fix

// before
const [first, ...rest] = apiResponse; // apiResponse is { items: [...] }
// TypeError: Invalid attempt to destructure non-iterable instance.

// after
const [first, ...rest] = apiResponse.items ?? [];
Defensive patterns

Strategy: type-guard

Validate before calling

// Verify iterability before array destructuring with a rest element.
const isIterable = (v) => v != null && typeof v[Symbol.iterator] === 'function';
const data = await fetchItems();
if (!isIterable(data)) throw new TypeError('fetchItems must return an iterable');
const [first, ...rest] = data;

Type guard

const isIterable = <T>(v: unknown): v is Iterable<T> & ArrayLike<T> =>
  v != null &&
  (typeof (v as Iterable<T>)[Symbol.iterator] === 'function' ||
    typeof (v as ArrayLike<T>).length === 'number');

Try / catch

try {
  const [first, ...rest] = maybeArray;
} catch (err) {
  if (err instanceof TypeError && /non-iterable/.test(err.message)) {
    // fall back to a sane default instead of crashing
    const [first, ...rest] = [];
  } else throw err;
}

Prevention

When it happens

Trigger: `const [first, ...rest] = v` where `v` is `{}`, a number, `null`, `undefined`, or a plain object without `[Symbol.iterator]`, compiled for targets that need the destructuring helpers.

Common situations: An API suddenly wraps its array in a pagination object (`{ items: [...] }`) and the destructuring site is not updated; destructuring `JSON.parse` output or `event.data` without a shape check; confusing a Map/Set with a plain object.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/712aef033874c13c. Report an issue: GitHub.