{"id":"c56093586ac3ea1b","repo":"sindresorhus/is","slug":"expected-value-which-is-non-negative-number-rec","errorCode":null,"errorMessage":"Expected value which is `non-negative number`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `non-negative number`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1785,"sourceCode":"\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 {\n\tif (!isNullOrUndefined(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('null or undefined', value));\n\t}\n}\n\nexport function assertNumber(value: unknown, message?: string): asserts value is number {","sourceCodeStart":1767,"sourceCodeEnd":1803,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1767-L1803","documentation":"Thrown by `assertNonNegativeNumber()` (source/index.ts:1783) when the value is not a number >= 0. Unlike the integer variant, fractional values like 0.5 pass, but negative numbers, `NaN`, non-numbers, and numeric strings fail.","triggerScenarios":"Calling `assert.nonNegativeNumber(value)` with a negative result from a subtraction (e.g. `endTime - startTime` with clock skew), `NaN` from parsing failures or `undefined` arithmetic, or a string that was never converted to a number.","commonSituations":"Duration/price/size calculations that go negative due to ordering bugs or clock adjustments; `Number(value)` on malformed input yielding NaN; amounts read from JSON where the field is a string.","solutions":["Convert with `Number(raw)` and verify it parsed before asserting.","Clamp legitimately-noisy values with `Math.max(0, value)` only if a small negative is a known measurement artifact — otherwise fix the ordering/subtraction bug producing it.","Trace NaN back to its source: an `undefined` operand or failed parse upstream."],"exampleFix":"// before\nconst duration = start - end; // operands swapped\nassert.nonNegativeNumber(duration);\n// after\nconst duration = end - start;\nassert.nonNegativeNumber(duration);","handlingStrategy":"validation","validationCode":"if (typeof value === 'number' && !Number.isNaN(value) && value >= 0) {\n  assert.nonNegativeNumber(value);\n}","typeGuard":"function isNonNegativeNumber(v: unknown): v is number {\n  return typeof v === 'number' && !Number.isNaN(v) && v >= 0;\n}","tryCatchPattern":"try {\n  assert.nonNegativeNumber(amount);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // amount is negative, NaN, or not a number — clamp with Math.max(0, n) only if that's semantically correct, otherwise reject\n  }\n  throw error;\n}","preventionTips":["Check value >= 0 explicitly on computed quantities (differences, balances, durations) before the call","NaN fails every comparison — validate parseFloat/Number() results before use","Numeric strings like '5' are not numbers to this library; convert and validate first"],"tags":["assertion","type-check","number","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}