{"record":{"id":"82abd9c0cc5f6f05","repo":"swc-project/swc","slug":"invalid-attempt-to-spread-non-iterable-instance-i","errorCode":null,"errorMessage":"Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.","messagePattern":"Invalid attempt to spread 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_spread.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_spread,\n    local: \"_non_iterable_spread\",\n    import_path: \"@swc/helpers/_/_non_iterable_spread\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _non_iterable_spread() {\n    throw new TypeError(\"Invalid attempt to spread 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(0x00000000000000080000000000000000),\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_spread.rs#L1-L24","documentation":"Array/call spread (`[...x]`, `f(...x)`) is lowered to `_to_consumable_array`, which accepts iterables and array-likes. When the spread operand is neither, the chain falls through to `_non_iterable_spread()`, throwing this TypeError — identical to what a native engine does when spreading a non-iterable.","triggerScenarios":"`[...obj]` or `fn(...obj)` where `obj` is a plain object, number, boolean, `null`, or `undefined` — anything without `[Symbol.iterator]` and without a numeric `length`.","commonSituations":"Spreading a plain object expecting its values (should be `Object.values`); spreading the result of an optional chain that is `undefined` (`[...(map && map.values())]` patterns); spreading older array-like DOM objects in transpiled output.","solutions":["For plain objects use `Object.keys/values/entries(obj)` instead of spread.","Guard nullable sources: `Array.from(x ?? [])`.","Convert array-likes explicitly with `Array.from(x)` before spreading.","Fix the producer so it returns an iterable where a spread is expected."],"exampleFix":"// before\nconst args = [...options]; // options = { verbose: true }\n// TypeError: Invalid attempt to spread non-iterable instance.\n\n// after\nconst args = Object.values(options ?? {});","handlingStrategy":"type-guard","validationCode":"// Check spread operands before `[...x]` or `f(...x)`.\nconst isSpreadable = (v) =>\n  v != null && (typeof v[Symbol.iterator] === 'function' || typeof v.length === 'number');\nconst merged = isSpreadable(extra) ? [...base, ...extra] : [...base];","typeGuard":"const isSpreadable = <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  args = [...source];\n} catch (err) {\n  if (err instanceof TypeError && /non-iterable/.test(err.message)) {\n    args = Object.values(source ?? {}); // graceful fallback for plain objects\n  } else throw err;\n}","preventionTips":["Prefer `Array.from(x ?? [])` at boundaries where the shape is uncertain.","Use `Object.values/entries` for plain objects — spread is for iterables only.","Add unit tests for the empty/null/undefined paths of every spread expression consuming external data."],"tags":["spread","iterable","es2015","typeerror"],"backgroundTag":"spread-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"}