antonmedv/fx · error · Error

Cannot reverse ${typeof x}

Error message

Cannot reverse ${typeof x}

What it means

reverse(x) reverses an array in place and only accepts arrays; any other type throws 'Cannot reverse <type>'. Strings must be split into an array first, objects have no defined reversal, and null/undefined are rejected outright.

Source

Thrown at internal/engine/stdlib.js:146

}

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) {
  if (Array.isArray(x)) {
    for (const y of x) console.log(y)
    return skip
  }
  throw new Error(`Cannot list ${typeof x}`)

View on GitHub (pinned to 4f31cd3a0c)

Solutions

  1. Check Array.isArray(x) before reversing
  2. For strings: x.split('').reverse().join('')
  3. For object key order: Object.keys(x).reverse()
  4. Default nullable inputs: reverse(x ?? [])

Example fix

// before
const rev = reverse(name)
// after
const rev = typeof name === 'string' ? name.split('').reverse().join('') : reverse(name ?? [])
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(x)) throw new TypeError('reverse() requires an array; got ' + (x === null ? 'null' : typeof x))

Type guard

function isReversible(x) { return Array.isArray(x) }

Try / catch

let rev; try { rev = reverse(x) } catch (e) { if (e.message.startsWith('Cannot reverse')) { rev = [] } else { throw e } }

Prevention

When it happens

Trigger: reverse('abc'), reverse({a:1}), reverse(null), reverse(undefined), or reverse on a scalar that a pipeline produced instead of a list.

Common situations: Reversing a string (need split('')/reverse/join('')), reversing object key order (need Object.keys), or a lookup returning null on absent data.

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


AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02). Data as JSON: /api/errors/fd38b847d4849d65. Report an issue: GitHub.