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
- Find and fix code that throws non-Error values — always `throw new Error(...)`
- When consuming third-party rejections, normalize first: `error instanceof Error ? error : new Error(String(error))`
- Rehydrate serialized errors (from JSON/IPC) into real Error instances before asserting
- 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
- In catch blocks the caught value is unknown — never assume it's an Error; normalize with value instanceof Error ? value : new Error(String(value))
- instanceof Error fails across realms (iframes, worker boundaries) — use a toString-based check if that applies
- Always throw Error instances (or subclasses) in your own code, never strings
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
- Expected value which is `empty map`, received value of type
- Expected value which is `empty object`, received value of ty
- Expected value which is `empty set`, received value of type
- Expected value which is `empty string`, received value of ty
- Expected value which is `empty string or whitespace`, receiv
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/042d92914912f967.json.
Report an issue: GitHub ↗.