{"id":"693f8dd7ec3ea653","repo":"sindresorhus/is","slug":"expected-value-which-is-in-range-received-value","errorCode":null,"errorMessage":"Expected value which is `in range`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `in range`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1671,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('GeneratorFunction', value));\n\t}\n}\n\nexport function assertHtmlElement(value: unknown, message?: string): asserts value is HTMLElement {\n\tif (!isHtmlElement(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('HTMLElement', value));\n\t}\n}\n\nexport function assertInfinite(value: unknown, message?: string): asserts value is number {\n\tif (!isInfinite(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('infinite number', value));\n\t}\n}\n\nexport function assertInRange(value: number, range: number | [number, number], message?: string): asserts value is number {\n\tif (!isInRange(value, range)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('in range', value));\n\t}\n}\n\nexport function assertInt16Array(value: unknown, message?: string): asserts value is Int16Array {\n\tif (!isInt16Array(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Int16Array', value));\n\t}\n}\n\nexport function assertInt32Array(value: unknown, message?: string): asserts value is Int32Array {\n\tif (!isInt32Array(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Int32Array', value));\n\t}\n}\n\nexport function assertInt8Array(value: unknown, message?: string): asserts value is Int8Array {\n\tif (!isInt8Array(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Int8Array', value));","sourceCodeStart":1653,"sourceCodeEnd":1689,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1653-L1689","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst port = Number(process.env.PORT);\nassertInRange(port, [1, 65535]); // NaN or 0 throws\n// after\nconst port = Number(process.env.PORT ?? 3000);\nif (Number.isNaN(port)) throw new Error('PORT must be numeric');\nassertInRange(port, [1, 65535]);","handlingStrategy":"validation","validationCode":"function inRange(value, range) {\n  const [min, max] = range.length === 2 ? [Math.min(...range), Math.max(...range)] : [0, range[0]];\n  return typeof value === 'number' && value >= min && value <= max;\n}\nif (!inRange(value, [0, 100])) {\n  throw new RangeError(`Value ${value} outside [0, 100]`);\n}","typeGuard":"function isInRange(value: unknown, min: number, max: number): value is number {\n  return typeof value === 'number' && !Number.isNaN(value) && value >= min && value <= max;\n}","tryCatchPattern":"try {\n  assert.inRange(value, [min, max]);\n} catch (error) {\n  if (error instanceof TypeError) { /* clamp, reject, or report the out-of-range input */ }\n  else throw error;\n}","preventionTips":["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]"],"tags":["number","range","input-validation","type-assertion"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}