immutable-js/immutable-js · error · Error

{}

Error message

{}

What it means

This error comes from the internal invariant() assertion helper: when the condition passed to it is falsy, it throws an Error with the supplied message. It guards preconditions across the library — the listed call sites are get, a constructor, setProp, and assertNotfinite — e.g. asserting that get() received a valid search value, that an index/count is not Infinity, and that internal object shapes are consistent. Seeing this error means an API was called with arguments that violate a documented precondition (or an internal state assumption was broken by misuse or a version mismatch).

Source

Thrown at src/utils/invariant.ts:5

export default function invariant(
  condition: unknown,
  error: string
): asserts condition {
  if (!condition) throw new Error(error);
}

View on GitHub (pinned to 59fdaae676)

Solutions

  1. Inspect the stack to identify which call site (get, constructor, setProp, assertNotInfinite) failed and validate that exact argument before the call
  2. Ensure get()/has() receive an actual key: state.get('key') not state.get(maybeUndefined)
  3. If it involves assertNotInfinite, fix the arithmetic producing Infinity/NaN in sizes, indices, or Range bounds
  4. Run npm ls immutable (or yarn why immutable) and dedupe to a single version — mixed versions commonly break internal constructor invariants
  5. Upgrade the library to a consistent patched version if the invariant fires on a known-buggy release

Example fix

// before
const id = req.query.id; // may be undefined
const item = map.get(id); // invariant can fail

// after
const id = req.query.id;
const item = id != null ? map.get(id) : undefined;
Defensive patterns

Strategy: validation

Validate before calling

const key = possiblyMissing;
const value = key !== undefined && key !== null ? map.get(key) : undefined;

// for numeric bounds:
const size = Number.isFinite(n) ? n : 0; // avoid Infinity reaching assertNotInfinite

Type guard

function isFiniteIndex(n: unknown): n is number {
  return typeof n === 'number' && Number.isFinite(n);
}

Try / catch

try {
  value = map.get(key);
} catch (e) {
  if (e instanceof Error && /assertNotInfinite|Cannot get/.test(e.message)) {
    value = undefined; // recover from bad input
  } else throw e;
}

Prevention

When it happens

Trigger: Calling collection.get() / .has() with no argument or an invalid key when an assertion requires a searchValue; constructing an Indexed Iterable or Range with infinite/NaN bounds that fail assertNotInfinite; setProp hitting an inconsistent internal record shape; mixing Immutable versions where internal symbols/flags checked in constructors differ, making the invariant condition falsy.

Common situations: Passing undefined to get() because a variable was never initialized (missing env var or missing config field); duplicated/peer-conflicting immutable packages in node_modules (npm or yarn hoisting issues) after an upgrade, causing constructor invariants to fail; computing sizes/limits with Infinity or NaN from bad math; calling methods with arguments built from unvalidated external input.

Related errors


AI-assisted analysis of immutable-js/immutable-js@59fdaae676 (2026-08-27). Data as JSON: /api/errors/1e7f09c79f920042. Report an issue: GitHub.