sindresorhus/is · error · TypeError

Expected value which is `URLSearchParams`, received value of

Error message

Expected value which is `URLSearchParams`, received value of type `${is(value)}`.

What it means

Thrown by `assertUrlSearchParams()` in @sindresorhus/is when the value is not a `URLSearchParams` instance. Query-string data represented as a raw string or plain object fails, because those need explicit construction into URLSearchParams.

Source

Thrown at source/index.ts:1963

	}
}

export function assertUndefined(value: unknown, message?: string): asserts value is undefined {
	if (!isUndefined(value)) {
		throw new TypeError(message ?? typeErrorMessage('undefined', value));
	}
}

export function assertUrlInstance(value: unknown, message?: string): asserts value is URL {
	if (!isUrlInstance(value)) {
		throw new TypeError(message ?? typeErrorMessage('URL', value));
	}
}

// eslint-disable-next-line unicorn/prevent-abbreviations
export function assertUrlSearchParams(value: unknown, message?: string): asserts value is URLSearchParams {
	if (!isUrlSearchParams(value)) {
		throw new TypeError(message ?? typeErrorMessage('URLSearchParams', value));
	}
}

export function assertUrlString(value: unknown, message?: string): asserts value is UrlString {
	if (!isUrlString(value)) {
		throw new TypeError(message ?? typeErrorMessage('string with a URL', value));
	}
}

export function assertValidDate(value: unknown, message?: string): asserts value is Date {
	if (!isValidDate(value)) {
		throw new TypeError(message ?? typeErrorMessage('valid Date', value));
	}
}

export function assertValidLength(value: unknown, message?: string): asserts value is number {
	if (!isValidLength(value)) {
		throw new TypeError(message ?? typeErrorMessage('valid length', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Construct it: `new URLSearchParams(value)` accepts a query string, an object of string pairs, or an array of pairs.
  2. Use `url.searchParams` instead of `url.search` when you already have a URL object.
  3. If you only need key/value semantics, assert a record/object shape instead of URLSearchParams.

Example fix

// before
assert.urlSearchParams(url.search);
// after
assert.urlSearchParams(url.searchParams);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is.urlSearchParams(value)) {
  value = new URLSearchParams(value); // from string, object, or entries
}

Type guard

if (is.urlSearchParams(value)) {
  // value is URLSearchParams here
}

Try / catch

try {
  assert.urlSearchParams(value);
} catch (error) {
  if (error instanceof TypeError) { /* got query string or plain object instead */ }
  throw error;
}

Prevention

When it happens

Trigger: `assert.urlSearchParams(value)` with a query string ("a=1&b=2"), a plain `{a: '1'}` object, a Map, or FormData.

Common situations: Framework request handlers exposing `req.query` as a plain object; passing `url.search` (a string) instead of `url.searchParams`; serialized params from fetch options.

Related errors


AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31). Data as JSON: /data/errors/eb0ab72c0f45bd57.json. Report an issue: GitHub ↗.