{"id":"3cc8dc1db9abd69e","repo":"sindresorhus/is","slug":"expected-value-which-is-non-negative-integer-re","errorCode":null,"errorMessage":"Expected value which is `non-negative integer`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `non-negative integer`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1779,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('non-empty set', value));\n\t}\n}\n\nexport function assertNonEmptyString(value: unknown, message?: string): asserts value is string {\n\tif (!isNonEmptyString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('non-empty string', value));\n\t}\n}\n\nexport function assertNonEmptyStringAndNotWhitespace(value: unknown, message?: string): asserts value is string {\n\tif (!isNonEmptyStringAndNotWhitespace(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('non-empty string and not whitespace', value));\n\t}\n}\n\nexport function assertNonNegativeInteger(value: unknown, message?: string): asserts value is number {\n\tif (!isNonNegativeInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('non-negative integer', value));\n\t}\n}\n\nexport function assertNonNegativeNumber(value: unknown, message?: string): asserts value is number {\n\tif (!isNonNegativeNumber(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('non-negative number', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertNull(value: unknown, message?: string): asserts value is null {\n\tif (!isNull(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('null', value));\n\t}\n}\n\n// eslint-disable-next-line @typescript-eslint/no-restricted-types\nexport function assertNullOrUndefined(value: unknown, message?: string): asserts value is null | undefined {","sourceCodeStart":1761,"sourceCodeEnd":1797,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1761-L1797","documentation":"Thrown by `assertNonNegativeInteger()` (source/index.ts:1777) when the value is not an integer >= 0. It rejects negative integers, non-integer numbers (1.5), `NaN`, `Infinity`, numeric strings like `'3'`, and BigInts. On success TypeScript narrows the value to `number`.","triggerScenarios":"Calling `assert.nonNegativeInteger(value)` with `-1` (common sentinel from `indexOf`-style APIs), a float from a division or average, `NaN` from failed arithmetic, or an unparsed string like `'42'` from query params or env vars.","commonSituations":"Passing `array.indexOf(x)` results directly (returns -1 on miss); counts or indexes computed with division; HTTP query/route params and env vars used without `Number.parseInt`; off-by-one decrements driving a counter below zero.","solutions":["Parse string inputs first: `Number.parseInt(raw, 10)`, then assert.","Check for the `-1` not-found sentinel before using an index from `indexOf`/`findIndex`.","Use `Math.floor`/`Math.round` deliberately if a fractional intermediate value is expected.","Guard arithmetic that can yield NaN (e.g. dividing by zero-length collections)."],"exampleFix":"// before\nconst page = req.query.page;\nassert.nonNegativeInteger(page); // '2' is a string — throws\n// after\nconst page = Number.parseInt(req.query.page ?? '0', 10);\nassert.nonNegativeInteger(page);","handlingStrategy":"validation","validationCode":"if (Number.isInteger(value) && value >= 0) {\n  assert.nonNegativeInteger(value);\n}","typeGuard":"function isNonNegativeInteger(v: unknown): v is number {\n  return typeof v === 'number' && Number.isInteger(v) && v >= 0;\n}","tryCatchPattern":"try {\n  assert.nonNegativeInteger(index);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // index is negative, fractional, NaN, or not a number — validate the arithmetic that produced it\n  }\n  throw error;\n}","preventionTips":["Parse strings with Number.parseInt(s, 10) and check Number.isInteger before passing counts/indexes","Watch for -1 sentinels from indexOf/findIndex flowing into APIs that require non-negative values","Guard division and subtraction results — they easily produce fractions or negatives","Remember NaN fails this check; validate anything derived from parsing external input"],"tags":["assertion","type-check","number","integer","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}