{"id":"dd4e9782c2b10aaf","repo":"sindresorhus/is","slug":"expected-value-which-is-safe-integer-received-v","errorCode":null,"errorMessage":"Expected value which is `safe integer`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `safe integer`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1878,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('Promise', value));\n\t}\n}\n\nexport function assertPropertyKey(value: unknown, message?: string): asserts value is PropertyKey {\n\tif (!isPropertyKey(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('PropertyKey', value));\n\t}\n}\n\nexport function assertRegExp(value: unknown, message?: string): asserts value is RegExp {\n\tif (!isRegExp(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('RegExp', value));\n\t}\n}\n\nexport function assertSafeInteger(value: unknown, message?: string): asserts value is number {\n\tif (!isSafeInteger(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('safe integer', value));\n\t}\n}\n\nexport function assertSet<T = unknown>(value: unknown, message?: string): asserts value is Set<T> {\n\tif (!isSet(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Set', value));\n\t}\n}\n\nexport function assertSharedArrayBuffer(value: unknown, message?: string): asserts value is SharedArrayBuffer {\n\tif (!isSharedArrayBuffer(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('SharedArrayBuffer', value));\n\t}\n}\n\nexport function assertString(value: unknown, message?: string): asserts value is string {\n\tif (!isString(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('string', value));","sourceCodeStart":1860,"sourceCodeEnd":1896,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1860-L1896","documentation":"Thrown by `assert.safeInteger()` (assertSafeInteger at source/index.ts:1876) when `isSafeInteger` (backed by `Number.isSafeInteger`) rejects the value. A safe integer is an integer in the range ±2^53−1 that can be exactly represented in an IEEE-754 double. Non-numbers, floats, NaN, Infinity, BigInt, and integers beyond ±9007199254740991 all fail.","triggerScenarios":"Calling `assert.safeInteger(value)` with a float (e.g. `1.5`), a numeric string (`'42'`), `NaN` from a failed `parseInt`, a BigInt, or an integer larger than `Number.MAX_SAFE_INTEGER` (common with 64-bit IDs from databases or APIs).","commonSituations":"Snowflake/64-bit IDs (Twitter, Discord, database bigint columns) parsed as numbers exceeding the safe range; form input or query params arriving as strings; arithmetic producing floats where integers were expected.","solutions":["If the value is a numeric string, parse it first: `Number.parseInt(value, 10)` and verify no NaN.","For 64-bit IDs beyond MAX_SAFE_INTEGER, keep them as strings or BigInt instead of asserting safe integer.","If floats are acceptable, use `assert.number()` instead; if any integer including unsafe ones, restructure to avoid precision loss."],"exampleFix":"// before\nconst id = Number(response.userId); // '9223372036854775807'\nassert.safeInteger(id);\n// after\nconst id = response.userId; // keep 64-bit IDs as strings\nassert.string(id);","handlingStrategy":"validation","validationCode":"if (typeof value !== 'number' || !Number.isSafeInteger(value)) {\n  throw new TypeError(`Expected a safe integer, got ${value}`);\n}","typeGuard":"function isSafeInteger(value: unknown): value is number {\n  return Number.isSafeInteger(value);\n}","tryCatchPattern":"try {\n  assert.safeInteger(value);\n} catch (error) {\n  if (error instanceof TypeError) {\n    // NaN, float, ±Infinity, or |value| > Number.MAX_SAFE_INTEGER\n  }\n  throw error;\n}","preventionTips":["Validate with Number.isSafeInteger before use — a plain integer check misses values beyond 2^53-1","Parse external numeric input (query strings, JSON with large IDs) carefully; large IDs silently lose precision as numbers — consider BigInt","Beware arithmetic that produces floats (division) or overflow before the assertion"],"tags":["typescript","type-assertion","number","integer-overflow","validation"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}