sindresorhus/is · error · TypeError
Expected value which is `in range`, received value of type `
Error message
Expected value which is `in range`, received value of type `${is(value)}`. What it means
Thrown by `assertInRange(value, range)` when `isInRange()` fails — the number falls outside the given bounds. The range parameter is either a single number (meaning 0 to that number) or a `[min, max]` tuple; bounds are inclusive and order-insensitive.
Source
Thrown at source/index.ts:1671
throw new TypeError(message ?? typeErrorMessage('GeneratorFunction', value));
}
}
export function assertHtmlElement(value: unknown, message?: string): asserts value is HTMLElement {
if (!isHtmlElement(value)) {
throw new TypeError(message ?? typeErrorMessage('HTMLElement', value));
}
}
export function assertInfinite(value: unknown, message?: string): asserts value is number {
if (!isInfinite(value)) {
throw new TypeError(message ?? typeErrorMessage('infinite number', value));
}
}
export function assertInRange(value: number, range: number | [number, number], message?: string): asserts value is number {
if (!isInRange(value, range)) {
throw new TypeError(message ?? typeErrorMessage('in range', value));
}
}
export function assertInt16Array(value: unknown, message?: string): asserts value is Int16Array {
if (!isInt16Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int16Array', value));
}
}
export function assertInt32Array(value: unknown, message?: string): asserts value is Int32Array {
if (!isInt32Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int32Array', value));
}
}
export function assertInt8Array(value: unknown, message?: string): asserts value is Int8Array {
if (!isInt8Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Int8Array', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Validate or clamp the input before asserting: `Math.min(Math.max(v, min), max)` if clamping is acceptable, otherwise reject with a domain-specific error.
- Check for NaN first (`is.number` + `!Number.isNaN`) since NaN produces this error with a confusing message.
- If negatives are valid, pass an explicit tuple `[min, max]` instead of the single-number shorthand.
Example fix
// before
const port = Number(process.env.PORT);
assertInRange(port, [1, 65535]); // NaN or 0 throws
// after
const port = Number(process.env.PORT ?? 3000);
if (Number.isNaN(port)) throw new Error('PORT must be numeric');
assertInRange(port, [1, 65535]); Defensive patterns
Strategy: validation
Validate before calling
function inRange(value, range) {
const [min, max] = range.length === 2 ? [Math.min(...range), Math.max(...range)] : [0, range[0]];
return typeof value === 'number' && value >= min && value <= max;
}
if (!inRange(value, [0, 100])) {
throw new RangeError(`Value ${value} outside [0, 100]`);
} Type guard
function isInRange(value: unknown, min: number, max: number): value is number {
return typeof value === 'number' && !Number.isNaN(value) && value >= min && value <= max;
} Try / catch
try {
assert.inRange(value, [min, max]);
} catch (error) {
if (error instanceof TypeError) { /* clamp, reject, or report the out-of-range input */ }
else throw error;
} Prevention
- Validate numeric inputs against the expected range at the trust boundary before calling the assert
- Clamp intentionally with `Math.min(Math.max(v, min), max)` when out-of-range values are acceptable
- NaN fails every range comparison — check `Number.isNaN` first
- Note the single-argument form `inRange(v, [end])` means range [0, end]
When it happens
Trigger: `is.assert.inRange(value, [min, max])` or `is.assert.inRange(value, max)` with a value outside the bounds — e.g. `assertInRange(150, [0, 100])`, `assertInRange(-1, 10)`, or with NaN, which fails every range comparison.
Common situations: Validating user input (ports, percentages, indices) without prior clamping; NaN from `parseInt`/`Number()` on bad input silently failing the range check; off-by-one expectations about inclusivity; forgetting the single-number form means `[0, n]`, so negative values always throw.
Related errors
- Expected value which is `even integer`, received value of ty
- Expected value which is `finite number`, received value of t
- Expected value which is `infinite number`, received value of
- Expected value which is `integer`, received value of type `$
- Expected value which is `NaN`, received value of type `${is(
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/693f8dd7ec3ea653.json.
Report an issue: GitHub ↗.