antonmedv/fx · error · Error

Cannot get unique values of ${typeof x}

Error message

Cannot get unique values of ${typeof x}

What it means

uniq(x) deduplicates array elements via a Set and only accepts arrays. Passing anything else — string, object, number, null, undefined — falls through to `throw new Error('Cannot get unique values of <type>')`. Note it does not even accept strings, so 'aab' must be split into characters first.

Source

Thrown at internal/engine/stdlib.js:35

}

const skip = Symbol('skip')

function apply(fn, ...args) {
  if (typeof fn === 'function') return fn(...args)
  return fn
}

function len(x) {
  if (Array.isArray(x)) return x.length
  if (typeof x === 'string') return x.length
  if (typeof x === 'object' && x !== null) return Object.keys(x).length
  throw new Error(`Cannot get length of ${typeof x}`)
}

function uniq(x) {
  if (Array.isArray(x)) return [...new Set(x)]
  throw new Error(`Cannot get unique values of ${typeof x}`)
}

function sort(x) {
  if (Array.isArray(x)) return x.sort()
  throw new Error(`Cannot sort ${typeof x}`)
}

function isFalsely(x) {
  return x === false || x === null || x === undefined
}

function filter(fn) {
  return function (x) {
    if (Array.isArray(x)) {
      return x.filter((v, i) => !isFalsely(fn(v, i)))
    }
    return isFalsely(fn(x)) ? skip : x
  }

View on GitHub (pinned to 4f31cd3a0c)

Solutions

  1. Ensure the input is an array; convert first with Array.from(x) or x.split(',') for strings
  2. Default null/undefined to an empty array: uniq(x ?? [])
  3. For strings, split into characters/items before deduplicating
  4. Guard the call site with Array.isArray before invoking uniq

Example fix

// before
const names = uniq(rawNames)
// after
const names = uniq(Array.isArray(rawNames) ? rawNames : String(rawNames ?? '').split(','))
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

let out; try { out = uniq(x) } catch (e) { if (e.message.startsWith('Cannot get unique values of')) { out = [] } else { throw e } }

Prevention

When it happens

Trigger: uniq('aabb'), uniq({a:1}), uniq(null), uniq(undefined), or uniq on the result of a function that returned a non-array (e.g. a lookup that yielded an object or null).

Common situations: Feeding uniq a comma-separated string instead of a split array, passing a JSON object where an array of records was expected, or null from an absent config/env value.

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/dea961584c774f63. Report an issue: GitHub.