sindresorhus/is · error · TypeError

Expected value which is `Error`, received value of type `${i

Error message

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

What it means

Thrown by `assertError` (source/index.ts:1596) when the value is not an `Error` instance (including subclasses like `TypeError`). It's commonly used in catch blocks, since TypeScript types caught values as `unknown`; if code throws a non-Error (string, plain object), this assertion rejects it with a TypeError naming the actual type.

Source

Thrown at source/index.ts:1598

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

export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {
	if (!isEmptyStringOrWhitespace(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));
	}
}

export function assertEnumCase<T = unknown>(value: unknown, targetEnum: T, message?: string): asserts value is T[keyof T] {
	if (!isEnumCase(value, targetEnum)) {
		throw new TypeError(message ?? typeErrorMessage('EnumCase', value));
	}
}

export function assertError(value: unknown, message?: string): asserts value is Error {
	if (!isError(value)) {
		throw new TypeError(message ?? typeErrorMessage('Error', value));
	}
}

export function assertEvenInteger(value: number, message?: string): asserts value is number {
	if (!isEvenInteger(value)) {
		throw new TypeError(message ?? typeErrorMessage('even integer', value));
	}
}

export function assertFalsy(value: unknown, message?: string): asserts value is Falsy {
	if (!isFalsy(value)) {
		throw new TypeError(message ?? typeErrorMessage('falsy', value));
	}
}

export function assertFiniteNumber(value: unknown, message?: string): asserts value is number {
	if (!isFiniteNumber(value)) {
		throw new TypeError(message ?? typeErrorMessage('finite number', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Find and fix code that throws non-Error values — always `throw new Error(...)`
  2. When consuming third-party rejections, normalize first: `error instanceof Error ? error : new Error(String(error))`
  3. Rehydrate serialized errors (from JSON/IPC) into real Error instances before asserting
  4. Use `is.error(value)` to branch gracefully instead of throwing on unknown rejection shapes

Example fix

// before
try { doWork(); } catch (error) { assert.error(error); } // throws if a string was thrown
// after
try { doWork(); } catch (error) {
  const err = error instanceof Error ? error : new Error(String(error));
  assert.error(err);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (value instanceof Error) {
  assert.error(value);
}

Type guard

function isError(value: unknown): value is Error {
  return value instanceof Error || Object.prototype.toString.call(value) === '[object Error]';
}

Try / catch

try {
  assert.error(value);
} catch (error) {
  if (error instanceof TypeError) {
    // caught value was not an Error — someone threw a string/object; wrap it: new Error(String(value))
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.error(value)` on a caught value that was thrown as a string or plain object, on an error-like object deserialized from JSON, or on an Error crossing realm boundaries (vm, iframe, worker) if the check relies on instanceof semantics.

Common situations: `catch (error) { assert.error(error); }` failing because third-party code does `throw 'message'` or rejects a promise with a plain object; error objects serialized over IPC/HTTP arriving as plain objects; axios/fetch libraries rejecting with custom non-Error shapes.

Related errors


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