{"record":{"id":"156cd33295b9e3a6","repo":"antonmedv/fx","slug":"cannot-flatten-typeof-x","errorCode":null,"errorMessage":"Cannot flatten ${typeof x}","messagePattern":"Cannot flatten (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"internal/engine/stdlib.js","lineNumber":141,"sourceCode":"    while (i < x.length) {\n      res.push(x.slice(i, i += size))\n    }\n    return res\n  }\n}\n\nfunction zip(...x) {\n  const length = Math.min(...x.map(a => a.length))\n  const res = []\n  for (let i = 0; i < length; i++) {\n    res.push(x.map(a => a[i]))\n  }\n  return res\n}\n\nfunction flatten(x) {\n  if (Array.isArray(x)) return x.flat()\n  throw new Error(`Cannot flatten ${typeof x}`)\n}\n\nfunction reverse(x) {\n  if (Array.isArray(x)) return x.reverse()\n  throw new Error(`Cannot reverse ${typeof x}`)\n}\n\nfunction keys(x) {\n  if (typeof x === 'object' && x !== null) return Object.keys(x)\n  throw new Error(`Cannot get keys of ${typeof x}`)\n}\n\nfunction values(x) {\n  if (typeof x === 'object' && x !== null) return Object.values(x)\n  throw new Error(`Cannot get values of ${typeof x}`)\n}\n\nfunction list(x) {","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/antonmedv/fx/blob/4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe/internal/engine/stdlib.js#L123-L159","documentation":"flatten(x) calls Array.prototype.flat() (one level deep) and only accepts arrays; any other type throws 'Cannot flatten <type>'. Strings, objects, null and undefined are all rejected — use explicit conversion if you need those handled.","triggerScenarios":"flatten({a:[1]}), flatten('abc'), flatten(null), flatten(undefined), or flatten on a value an upstream step returned as an object/scalar instead of an array.","commonSituations":"Grouping results (groupBy output is an object, not an array) then flattening, JSON APIs returning an object wrapper instead of an array, or null from a missing config key.","solutions":["Confirm the input is an array with Array.isArray before flattening","If you have an object of arrays, flatten its values: Object.values(x).flat()","Default nullable inputs: flatten(x ?? [])","For deeper nesting combine with map: x.map(v => Array.isArray(v) ? v : [v]).flat()"],"exampleFix":"// before\nconst all = flatten(grouped)\n// after\nconst all = flatten(Object.values(grouped))","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(x)) throw new TypeError('flatten() requires an array; got ' + (x === null ? 'null' : typeof x))","typeGuard":"function isFlattenable(x) { return Array.isArray(x) }","tryCatchPattern":"let flat; try { flat = flatten(x) } catch (e) { if (e.message.startsWith('Cannot flatten')) { flat = [] } else { throw e } }","preventionTips":["For grouped objects use Object.values(x).flat() before flattening","Default nullables: flatten(x ?? [])","Remember flat() is one level deep — plan nesting accordingly","Validate shapes after groupBy/map stages"],"tags":["type-error","runtime","stdlib"],"backgroundTag":"invalid-argument-value","analyzedSha":"4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe","analyzedAt":"2026-09-02T02:17:47.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}