zloirock/core-js · error · TypeError

Expected sequence with length 2

Error message

Expected sequence with length 2

What it means

TypeError thrown by the URLSearchParams polyfill constructor when initializing from a sequence-of-sequences: each entry must be an iterable yielding exactly two values (key, value). The code pulls three iterator steps and requires the first two done===false and the third done===true; otherwise 'Expected sequence with length 2' is thrown.

Source

Thrown at packages/core-js/modules/web.url-search-params.constructor.js:138

    this.url = url;
    this.update();
  },
  parseObject: function (object) {
    var entries = this.entries;
    var iteratorMethod = getIteratorMethod(object);
    var iterator, next, step, entryIterator, entryNext, first, second;

    if (iteratorMethod) {
      iterator = getIterator(object, iteratorMethod);
      next = iterator.next;
      while (!(step = call(next, iterator)).done) {
        entryIterator = getIterator(anObject(step.value));
        entryNext = entryIterator.next;
        if (
          (first = call(entryNext, entryIterator)).done ||
          (second = call(entryNext, entryIterator)).done ||
          !call(entryNext, entryIterator).done
        ) throw new TypeError('Expected sequence with length 2');
        push(entries, { key: $toString(first.value), value: $toString(second.value) });
      }
    } else for (var key in object) if (hasOwn(object, key)) {
      push(entries, { key: key, value: $toString(object[key]) });
    }
  },
  parseQuery: function (query) {
    if (query) {
      var entries = this.entries;
      var attributes = split(query, '&');
      var index = 0;
      var attribute, entry;
      while (index < attributes.length) {
        attribute = attributes[index++];
        if (attribute.length) {
          entry = split(attribute, '=');
          push(entries, {
            key: decodeQueryComponent(shift(entry)),

View on GitHub (pinned to 84e45fba09)

Solutions

  1. Ensure every entry is a [key, value] pair of exactly two elements
  2. Map/normalize rows: entries.map(([k, v]) => [k, v]) or entries.map(r => [r[0], r[1]])
  3. Use an object or a Map instead of raw sequences when arity is uncertain

Example fix

// before
new URLSearchParams([['a', 'b', 'extra']]); // TypeError: Expected sequence with length 2
// after
new URLSearchParams([['a', 'b']]);
Defensive patterns

Strategy: validation

Validate before calling

function toPairs(input) {
  if (Array.isArray(input)) return input.map(r => [String(r[0]), String(r[1])]);
  if (input instanceof Map) return [...input].map(([k, v]) => [String(k), String(v)]);
  return Object.entries(input);
}
const params = new URLSearchParams(toPairs(raw));

Type guard

const isPair = (e) =>
  e != null && typeof e[Symbol.iterator] === 'function' &&
  [...e].length === 2;

Try / catch

try {
  p = new URLSearchParams(seq);
} catch (e) {
  if (e instanceof TypeError && /Expected sequence with length 2/.test(e.message)) {
    p = new URLSearchParams(seq.map(r => [r[0], r[1]]));
  } else throw e;
}

Prevention

When it happens

Trigger: new URLSearchParams([[k]], [[k,v,extra]] or new URLSearchParams([['a','b','c']]) — any inner entry whose iterator yields fewer than 2 or more than 2 values.

Common situations: Passing Object.entries-like arrays that contain 3-element rows; spreading a Map with tuple-ish arrays of wrong arity; a row that is a string of length != 2 (iterating a string yields chars); mistyped rows after data transformation.

Related errors


AI-assisted analysis of zloirock/core-js@84e45fba09 (2026-08-30). Data as JSON: /api/errors/58b1f0147a7b05d9. Report an issue: GitHub.