sindresorhus/is · error · TypeError
Expected value which is `number`, received value of type `${
Error message
Expected value which is `number`, received value of type `${is(value)}`. What it means
Thrown by `assert.number()` in the `@sindresorhus/is` library when the given value is not of primitive type `number`. The assert family wraps the `is.*` predicates and throws a TypeError so the value can be safely narrowed via TypeScript's `asserts value is number`. The message interpolates `is(value)` to report the actual detected type of what was received.
Source
Thrown at source/index.ts:1805
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertNull(value: unknown, message?: string): asserts value is null {
if (!isNull(value)) {
throw new TypeError(message ?? typeErrorMessage('null', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertNullOrUndefined(value: unknown, message?: string): asserts value is null | undefined {
if (!isNullOrUndefined(value)) {
throw new TypeError(message ?? typeErrorMessage('null or undefined', value));
}
}
export function assertNumber(value: unknown, message?: string): asserts value is number {
if (!isNumber(value)) {
throw new TypeError(message ?? typeErrorMessage('number', value));
}
}
export function assertNumericString(value: unknown, message?: string): asserts value is `${number}` {
if (!isNumericString(value)) {
throw new TypeError(message ?? typeErrorMessage('string with a number', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertObject(value: unknown, message?: string): asserts value is object {
if (!isObject(value)) {
throw new TypeError(message ?? typeErrorMessage('Object', value));
}
}
export function assertObservable(value: unknown, message?: string): asserts value is ObservableLike {
if (!isObservable(value)) {View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Convert the value first, e.g. `const n = Number(raw)` before `assert.number(n)`.
- If the input is legitimately a numeric string, use `assert.numericString()` instead.
- Log `is(value)` from the error message to identify what the caller actually passed and fix the producer.
- If undefined is allowed, guard with `if (value !== undefined) assert.number(value)`.
Example fix
// before const port = process.env.PORT; assert.number(port); // throws: received 'string' // after const port = Number(process.env.PORT); assert.number(port);
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof value !== 'number' || Number.isNaN(value)) {
throw new TypeError(`Expected number, got ${typeof value}`);
} Type guard
function isNumber(value: unknown): value is number {
return typeof value === 'number' && !Number.isNaN(value);
} Try / catch
try {
ow(value, ow.number);
} catch (error) {
if (error instanceof ArgumentError) {
// report invalid input to caller; do not substitute a default
throw new TypeError(`Invalid numeric input: ${error.message}`);
}
throw error;
} Prevention
- Convert user/string input with Number() and check Number.isNaN before passing to the API
- Remember typeof NaN === 'number' — decide explicitly whether NaN is acceptable
- Type external data as unknown and narrow with a guard before calling the library
- Avoid implicit coercion like +value or value * 1 hiding non-number inputs
When it happens
Trigger: Calling `assert.number(value)` (or `assertNumber`) with anything whose typeof is not 'number' — strings like '42', BigInt, null, undefined, NaN is accepted since typeof NaN === 'number'. Common at API boundaries where JSON/query params arrive as strings.
Common situations: Parsing query strings, env vars, or form inputs where numbers arrive as strings; forgetting to call Number()/parseFloat before validation; a config file storing a number as a quoted string; upstream API changing a field from number to string.
Related errors
- Expected value which is `non-empty array`, received value of
- Expected value which is `non-empty map`, received value of t
- Expected value which is `non-empty object`, received value o
- Expected value which is `non-empty set`, received value of t
- Expected value which is `non-empty string`, received value o
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/1baf61f3d84eca0b.json.
Report an issue: GitHub ↗.