sindresorhus/is · error · TypeError

Expected value which is `URL`, received value of type `${is(

Error message

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

What it means

Thrown by `assertUrlInstance()` in @sindresorhus/is when the value is not a `URL` object instance. It checks for the URL class specifically — a valid URL *string* still fails, because strings are covered by the separate `urlString` guard.

Source

Thrown at source/index.ts:1956

		throw new TypeError(message ?? typeErrorMessage('Uint8Array', value));
	}
}

export function assertUint8ClampedArray(value: unknown, message?: string): asserts value is Uint8ClampedArray {
	if (!isUint8ClampedArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('Uint8ClampedArray', value));
	}
}

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)) {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Wrap strings: `new URL(value)` before asserting/using.
  2. If a URL string is what you actually accept, use `assert.urlString(value)` instead.
  3. At API boundaries accept `string | URL` and normalize once: `const url = value instanceof URL ? value : new URL(value)`.

Example fix

// before
assert.urlInstance(process.env.API_URL);
// after
assert.urlString(process.env.API_URL);
const url = new URL(process.env.API_URL);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is.urlInstance(value)) {
  value = new URL(String(value)); // may itself throw on invalid URL strings
}

Type guard

if (is.urlInstance(value)) {
  // value is URL here
}

Try / catch

try {
  assert.urlInstance(value);
} catch (error) {
  if (error instanceof TypeError) { /* got a string or other non-URL */ }
  throw error;
}

Prevention

When it happens

Trigger: `assert.urlInstance(value)` called with a string like "https://example.com", a Node legacy `url.parse()` result, a Location object, or a plain object with href/host fields.

Common situations: Config values and env vars are strings and get passed straight to code expecting a URL object; mixing Node's legacy `url` module output with WHATWG URL; JSON-deserialized data where a URL became its href string.

Related errors


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