{"id":"5c993ad93796e835","repo":"sindresorhus/is","slug":"expected-value-which-is-negative-integer-receiv","errorCode":null,"errorMessage":"Expected value which is `negative integer`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `negative integer`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1725,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('Map', value));\n\t}\n}\n\nexport function assertNan(value: unknown, message?: string): asserts value is number {\n\tif (!isNan(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('NaN', value));\n\t}\n}\n\nexport function assertNativePromise<T = unknown>(value: unknown, message?: string): asserts value is Promise<T> {\n\tif (!isNativePromise(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('native Promise', value));\n\t}\n}\n\nexport function assertNegativeInteger(value: unknown, message?: string): asserts value is number {\n\tif (!isNegativeInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('negative integer', value));\n\t}\n}\n\nexport function assertNegativeNumber(value: unknown, message?: string): asserts value is number {\n\tif (!isNegativeNumber(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('negative number', value));\n\t}\n}\n\nexport function assertNodeStream(value: unknown, message?: string): asserts value is NodeStream {\n\tif (!isNodeStream(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Node.js Stream', value));\n\t}\n}\n\nexport function assertNonEmptyArray<T = unknown, Item = unknown>(value: T | Item[], message?: string): asserts value is [Item, ...Item[]] {\n\tif (!isNonEmptyArray(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('non-empty array', value));","sourceCodeStart":1707,"sourceCodeEnd":1743,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1707-L1743","documentation":"Thrown by `assertNegativeInteger()` (source/index.ts:1723) when the value fails `isInteger(value) && value < 0`. Both conditions must hold: a number primitive, whole, and strictly less than zero. Zero, positive integers, negative floats, and numeric strings all fail.","triggerScenarios":"Calling `assertNegativeInteger()` with 0 (not negative), a positive integer, -3.5 (not an integer), '-5' (string), NaN, or -Infinity (not an integer).","commonSituations":"Off-by-one boundary confusion where 0 was expected to pass; string inputs from CLI/env/query params; results of subtraction that yield floats due to floating-point arithmetic; using -0 works (Number.isInteger(-0) && -0 < 0 is false — -0 fails, another boundary trap).","solutions":["Parse strings first: `Number.parseInt(raw, 10)` before asserting.","If zero is valid in your domain, use `assertNonPositiveNumber()`/adjust the check rather than negative-integer.","Truncate intentional fractional values with `Math.trunc()` if whole negatives are the real requirement.","Trace upstream arithmetic if NaN/-Infinity reaches the assert."],"exampleFix":"// before\nconst offset = Number(raw) / 3; // may be -1.6667\nassertNegativeInteger(offset); // throws\n// after\nconst offset = Math.trunc(Number(raw) / 3);\nassertNegativeInteger(offset);","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(value) || value >= 0) {\n  throw new RangeError(`Expected negative integer, got ${value}`);\n}","typeGuard":"const isNegativeInteger = (v: unknown): v is number => typeof v === 'number' && Number.isInteger(v) && v < 0;","tryCatchPattern":null,"preventionTips":["This check compounds two conditions — a positive integer and a negative float both fail; validate both integer-ness and sign","Zero and -0 are not negative; if -0 should count, decide explicitly (v < 0 excludes -0)","Validate sign at the trust boundary where the number is produced, not deep in call chains"],"tags":["number","validation","type-assertion"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}