{"record":{"id":"ecb55ecd40d5df4b","repo":"swc-project/swc","slug":"object-is-not-iterable","errorCode":null,"errorMessage":"Object is not iterable.","messagePattern":"Object is not iterable\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_base/src/helpers/generated/_ts_values.rs","lineNumber":25,"sourceCode":"    local: \"_ts_values\",\n    import_path: \"@swc/helpers/_/_ts_values\",\n    #[cfg(feature = \"inline-helpers\")]\n    source: r#\"function _ts_values(o) {\n    var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\n    if (m) {\n        return m.call(o);\n    }\n    if (o && typeof o.length === \"number\") {\n        return {\n            next: function() {\n                if (o && i >= o.length) {\n                    o = void 0;\n                }\n                return { value: o && o[i++], done: !o };\n            }\n        };\n    }\n    throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\n}\n\"#,\n    #[cfg(feature = \"inline-helpers\")]\n    deps: super::HelperBitmap::from_bits(0x00000001000000000000000000000000),\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":7,"sourceCodeEnd":38,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_base/src/helpers/generated/_ts_values.rs#L7-L38","documentation":"`_ts_values` is the iteration helper emitted for `for...of` (and iterable destructuring) when TypeScript-style downlevel iteration is compiled by SWC. It accepts real iterables (`typeof o[Symbol.iterator] === 'function'`) or array-likes (`typeof o.length === 'number'`); anything else triggers `TypeError: Object is not iterable.`","triggerScenarios":"`for (const x of obj)` where `obj` is a plain object (no `[Symbol.iterator]`, no `.length`) in output compiled with downlevel iteration helpers — typically TS with target below ES2015 or `downlevelIteration` semantics.","commonSituations":"Iterating a plain object expecting entries instead of using `Object.entries`; an API changing an array response to an object; DOM/custom collections without iterator support; Map/Set confused with plain objects.","solutions":["Use `Object.entries(obj)` / `Object.keys` / `Object.values` for plain-object iteration.","Normalize before the loop: `for (const x of Array.from(obj))` when the value is array-like.","Add an iterator to your custom class: `*[Symbol.iterator]() { ... }`.","Fix the data source to hand the loop an actual array/iterable."],"exampleFix":"// before\nfor (const item of config) {} // config = { a: 1 }\n// TypeError: Object is not iterable.\n\n// after\nfor (const [key, value] of Object.entries(config ?? {})) {}","handlingStrategy":"type-guard","validationCode":"// Mirror _ts_values: accept iterables or array-likes before for-of.\nconst isIterableOrArrayLike = (v) =>\n  v != null &&\n  (typeof v[Symbol.iterator] === 'function' || typeof v.length === 'number');\nconst source = await getData();\nif (!isIterableOrArrayLike(source)) throw new TypeError(`expected iterable, got ${typeof source}`);\nfor (const x of source) { /* ... */ }","typeGuard":"const isIterableOrArrayLike = <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  for (const x of obj) { handle(x); }\n} catch (err) {\n  if (err instanceof TypeError && /not iterable/.test(err.message)) {\n    for (const value of Object.values(obj ?? {})) { handle(value); }\n  } else throw err;\n}","preventionTips":["Use `Object.entries/keys/values` for plain-object iteration — never `for...of` directly.","Validate external payloads with a schema so shape changes (array to object) fail early with context.","Give custom collections a `*[Symbol.iterator]()` method if they should participate in `for...of`."],"tags":["for-of","iterable","downlevel-iteration","typescript","typeerror"],"backgroundTag":"for-of-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"}