sindresorhus/is · warning · TypeError
Expected value which is `NaN`, received value of type `${is(
Error message
Expected value which is `NaN`, received value of type `${is(value)}`. What it means
Thrown by `assertNan()` (source/index.ts:1711) when `Number.isNaN(value)` is false — this assert requires the value to literally BE NaN, the opposite of the usual 'not NaN' check. It exists for code paths (tests, sentinel handling) that expect a NaN result.
Source
Thrown at source/index.ts:1713
throw new TypeError(message ?? typeErrorMessage('integer', value));
}
}
export function assertIterable<T = unknown>(value: unknown, message?: string): asserts value is Iterable<T> {
if (!isIterable(value)) {
throw new TypeError(message ?? typeErrorMessage('Iterable', value));
}
}
export function assertMap<Key = unknown, Value = unknown>(value: unknown, message?: string): asserts value is Map<Key, Value> {
if (!isMap(value)) {
throw new TypeError(message ?? typeErrorMessage('Map', value));
}
}
export function assertNan(value: unknown, message?: string): asserts value is number {
if (!isNan(value)) {
throw new TypeError(message ?? typeErrorMessage('NaN', value));
}
}
export function assertNativePromise<T = unknown>(value: unknown, message?: string): asserts value is Promise<T> {
if (!isNativePromise(value)) {
throw new TypeError(message ?? typeErrorMessage('native Promise', value));
}
}
export function assertNegativeInteger(value: unknown, message?: string): asserts value is number {
if (!isNegativeInteger(value)) {
throw new TypeError(message ?? typeErrorMessage('negative integer', value));
}
}
export function assertNegativeNumber(value: unknown, message?: string): asserts value is number {
if (!isNegativeNumber(value)) {
throw new TypeError(message ?? typeErrorMessage('negative number', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- If you meant 'must be a real, usable number', use `assertNumber()` (which in this library excludes NaN) instead of `assertNan()`.
- If you genuinely expect NaN, check why the computation produced a real value instead (e.g. 1/0 gives Infinity, not NaN).
- Remember `assertNan` uses non-coercing `Number.isNaN` — pass the numeric result, not a raw string.
Example fix
// before — intended to validate a usable number assertNan(input); // throws for every valid number! // after assertNumber(input); // excludes NaN in this library
Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isNaN(value)) {
// value is a real number or non-number — this assert expects literally NaN
} Type guard
const isNaNValue = (v: unknown): v is number => typeof v === 'number' && Number.isNaN(v);
Prevention
- assert.nan expects the value to BE NaN — only use it when asserting a computation intentionally produced NaN
- Use Number.isNaN, not global isNaN, which coerces and returns true for non-numeric strings
- NaN !== NaN, so never compare with ===; Number.isNaN or Object.is(v, NaN) are the correct checks
When it happens
Trigger: Calling `assertNan()` with any valid number, a non-number (strings never pass — `Number.isNaN` does not coerce, so even 'foo' fails), null, or undefined.
Common situations: The most common hit is a semantic mix-up: developers call `assertNan(value)` intending to assert the value is a number that is NOT NaN. Also tests asserting a computation produced NaN when it actually produced Infinity or 0, and expecting the coercing global `isNaN('foo') === true` behavior — `Number.isNaN('foo')` is false.
Related errors
- Expected value which is `finite number`, received value of t
- Expected value which is `even integer`, received value of ty
- Expected value which is `infinite number`, received value of
- Expected value which is `in range`, received value of type `
- Expected value which is `integer`, received value of type `$
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/e1312ddba59e66e5.json.
Report an issue: GitHub ↗.