sindresorhus/is · error · TypeError
Expected value which is `valid length`, received value of ty
Error message
Expected value which is `valid length`, received value of type `${is(value)}`. What it means
Thrown by `assertValidLength()` in @sindresorhus/is when the value fails `isValidLength`, i.e. it is not a safe integer >= 0 (`Number.isSafeInteger(value) && value >= 0`). It validates values meant to be used as array/string lengths or counts.
Source
Thrown at source/index.ts:1981
throw new TypeError(message ?? typeErrorMessage('URLSearchParams', value));
}
}
export function assertUrlString(value: unknown, message?: string): asserts value is UrlString {
if (!isUrlString(value)) {
throw new TypeError(message ?? typeErrorMessage('string with a URL', value));
}
}
export function assertValidDate(value: unknown, message?: string): asserts value is Date {
if (!isValidDate(value)) {
throw new TypeError(message ?? typeErrorMessage('valid Date', value));
}
}
export function assertValidLength(value: unknown, message?: string): asserts value is number {
if (!isValidLength(value)) {
throw new TypeError(message ?? typeErrorMessage('valid length', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {
if (!isWeakMap(value)) {
throw new TypeError(message ?? typeErrorMessage('WeakMap', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakRef<T extends object = object>(value: unknown, message?: string): asserts value is WeakRef<T> {
if (!isWeakRef(value)) {
throw new TypeError(message ?? typeErrorMessage('WeakRef', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-restricted-typesView on GitHub ↗ (pinned to 7821031c66)
Solutions
- Convert and round appropriately: `Math.floor(value)` or `Math.trunc(value)` for computed lengths, and clamp with `Math.max(0, value)` if underflow is expected.
- Parse strings to numbers first: `Number.parseInt(str, 10)` and validate the result isn't NaN.
- Find the arithmetic that produced the negative/fractional value — the assertion usually flags an upstream logic bug (off-by-one, division), not a formatting problem.
Example fix
// before const pages = total / pageSize; assert.validLength(pages); // after const pages = Math.ceil(total / pageSize); assert.validLength(pages);
Defensive patterns
Strategy: validation
Validate before calling
function isValidLength(value) {
return Number.isSafeInteger(value) && value >= 0;
}
if (!isValidLength(len)) {
throw new RangeError(`Invalid length: ${len}`);
} Type guard
if (is.validLength(value)) {
// value is a non-negative safe integer
} Try / catch
try {
assert.validLength(value);
} catch (error) {
if (error instanceof TypeError) { /* negative, fractional, NaN, or unsafe integer */ }
throw error;
} Prevention
- A valid length is a non-negative safe integer — guard against NaN, negatives, fractions, and Infinity from arithmetic
- Validate lengths derived from user input or division/subtraction before using them to size arrays or slices
- Use Number.isSafeInteger(n) && n >= 0 as the equivalent pre-check
When it happens
Trigger: `assert.validLength(value)` with a negative number, a float (e.g. 2.5), NaN, Infinity, a number beyond Number.MAX_SAFE_INTEGER, a numeric string like '5', or a BigInt.
Common situations: Lengths computed by division producing floats (`total / pageSize`); parsing lengths from headers or query strings without Number conversion; offset arithmetic underflowing to negative values; Content-Length beyond safe-integer range.
Related errors
- Expected value which is `safe integer`, received value of ty
- Expected value which is `finite number`, received value of t
- Expected value which is `integer`, received value of type `$
- Expected value which is `negative integer`, received value o
- Expected value which is `negative number`, received value of
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/db73ec21b3de0ae8.json.
Report an issue: GitHub ↗.