{"record":{"id":"712aef033874c13c","repo":"swc-project/swc","slug":"invalid-attempt-to-destructure-non-iterable-instan","errorCode":null,"errorMessage":"Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.","messagePattern":"Invalid attempt to destructure non-iterable instance\\.\nIn order to be iterable, non-array objects must have a \\[Symbol\\.iterator\\]\\(\\) method\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_non_iterable_rest.rs","lineNumber":11,"sourceCode":"// This file is generated by `cargo codegen helpers`. DO NOT MODIFY.\n\nuse super::{HelperDef, HelperName};\n\npub const DEF: HelperDef = HelperDef {\n    name: HelperName::non_iterable_rest,\n    local: \"_non_iterable_rest\",\n    import_path: \"@swc/helpers/_/_non_iterable_rest\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _non_iterable_rest() {\n    throw new TypeError(\"Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\");\n}\n\"#,\n    #[cfg(feature = \"inline-helpers\")]\n    deps: super::HelperBitmap::from_bits(0x00000000000000040000000000000000),\n};\n\n#[cfg(feature = \"inline-helpers\")]\npub fn stmts() -> &'static [swc_ecma_ast::Stmt] {\n    static STMTS: once_cell::sync::Lazy<Vec<swc_ecma_ast::Stmt>> =\n        once_cell::sync::Lazy::new(|| super::super::parse(DEF.source, DEF.import_path));\n    &STMTS\n}\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_non_iterable_rest.rs#L1-L24","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Validate the value is iterable before destructuring: `if (v != null && typeof v[Symbol.iterator] === 'function')`.","If the value may be array-like only, normalize first with `Array.from(v)`.","Fix the data source so the destructuring site actually receives an array (e.g. destructure `resp.items`, not `resp`).","Provide a safe default at the boundary: `const [first, ...rest] = expectedArray ?? [];`"],"exampleFix":"// before\nconst [first, ...rest] = apiResponse; // apiResponse is { items: [...] }\n// TypeError: Invalid attempt to destructure non-iterable instance.\n\n// after\nconst [first, ...rest] = apiResponse.items ?? [];","handlingStrategy":"type-guard","validationCode":"// Verify iterability before array destructuring with a rest element.\nconst isIterable = (v) => v != null && typeof v[Symbol.iterator] === 'function';\nconst data = await fetchItems();\nif (!isIterable(data)) throw new TypeError('fetchItems must return an iterable');\nconst [first, ...rest] = data;","typeGuard":"const isIterable = <T>(v: unknown): v is Iterable<T> & ArrayLike<T> =>\n  v != null &&\n  (typeof (v as Iterable<T>)[Symbol.iterator] === 'function' ||\n    typeof (v as ArrayLike<T>).length === 'number');","tryCatchPattern":"try {\n  const [first, ...rest] = maybeArray;\n} catch (err) {\n  if (err instanceof TypeError && /non-iterable/.test(err.message)) {\n    // fall back to a sane default instead of crashing\n    const [first, ...rest] = [];\n  } else throw err;\n}","preventionTips":["Type function returns precisely (`T[]`, not `any`) so destructuring mismatches surface at compile time.","Normalize at API boundaries: wrap parsed JSON in schema validation (zod/valibot) before destructuring.","Reach for `Array.from(v)` when the value may be array-like rather than truly iterable."],"tags":["destructuring","rest-element","iterable","es2015","typeerror"],"backgroundTag":"destructuring-non-iterable","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}