antonmedv/fx · error · Error
Cannot flatten ${typeof x}
Error message
Cannot flatten ${typeof x} What it means
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.
Source
Thrown at internal/engine/stdlib.js:141
while (i < x.length) {
res.push(x.slice(i, i += size))
}
return res
}
}
function zip(...x) {
const length = Math.min(...x.map(a => a.length))
const res = []
for (let i = 0; i < length; i++) {
res.push(x.map(a => a[i]))
}
return res
}
function flatten(x) {
if (Array.isArray(x)) return x.flat()
throw new Error(`Cannot flatten ${typeof x}`)
}
function reverse(x) {
if (Array.isArray(x)) return x.reverse()
throw new Error(`Cannot reverse ${typeof x}`)
}
function keys(x) {
if (typeof x === 'object' && x !== null) return Object.keys(x)
throw new Error(`Cannot get keys of ${typeof x}`)
}
function values(x) {
if (typeof x === 'object' && x !== null) return Object.values(x)
throw new Error(`Cannot get values of ${typeof x}`)
}
function list(x) {View on GitHub (pinned to 4f31cd3a0c)
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()
Example fix
// before const all = flatten(grouped) // after const all = flatten(Object.values(grouped))
Defensive patterns
Strategy: type-guard
Validate before calling
if (!Array.isArray(x)) throw new TypeError('flatten() requires an array; got ' + (x === null ? 'null' : typeof x)) Type guard
function isFlattenable(x) { return Array.isArray(x) } Try / catch
let flat; try { flat = flatten(x) } catch (e) { if (e.message.startsWith('Cannot flatten')) { flat = [] } else { throw e } } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot get length of ${typeof x}
- Cannot get unique values of ${typeof x}
- Cannot sort ${typeof x}
- Cannot reverse ${typeof x}
- Cannot get keys of ${typeof x}
AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02).
Data as JSON: /api/errors/156cd33295b9e3a6.
Report an issue: GitHub.