antonmedv/fx · error · Error

Cannot get keys of ${typeof x}

Error message

Cannot get keys of ${typeof x}

What it means

keys(x) returns Object.keys and accepts any non-null object — arrays count as objects, so keys([10,20]) returns ['0','1']. It throws 'Cannot get keys of <type>' for primitives (string, number, boolean, undefined) and for null. The null rejection is the common surprise: a missing/failed lookup yields null and this throws.

Source

Thrown at internal/engine/stdlib.js:151

  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}`)
}

function del(key) {
  return function (x) {
    if (Array.isArray(x)) {

View on GitHub (pinned to 4f31cd3a0c)

Solutions

  1. Coerce null to an empty object: keys(x ?? {})
  2. Check x != null before calling keys
  3. If x may be a string and you want its characters, split first: x.split('')
  4. Wrap in try/catch when the input type is uncertain

Example fix

// before
const ks = keys(cfg.network)
// after
const ks = keys(cfg?.network ?? {})
Defensive patterns

Strategy: fallback

Validate before calling

if (x == null || typeof x !== 'object') throw new TypeError('keys() requires a non-null object; got ' + (x === null ? 'null' : typeof x))

Type guard

function isKeyed(x) { return x !== null && typeof x === 'object' }

Try / catch

let ks; try { ks = keys(x) } catch (e) { if (e.message.startsWith('Cannot get keys of')) { ks = [] } else { throw e } }

Prevention

When it happens

Trigger: keys(null), keys('abc'), keys(42), keys(undefined), keys(true) — null is the most frequent since JSON lookups commonly return null for absent data.

Common situations: Accessing a config/env object that failed to load (null), API responses with a missing payload, or accidentally passing a string when an object was intended.

Related errors


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