Aller au contenu principal

isSafeInteger()

isSafeInteger(value): value is number

Checks if value is a safe integer.

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number, and whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.

DEPRECATED

Use Number.isSafeInteger() directly instead.


Parameters

value: unknown

The value to check.


Returns: value is number

true if value is a safe integer, else false.


See Also


Since

2.0.0


Also known as

isSafeInteger (Lodash, es-toolkit) · ❌ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Example

// ❌ Deprecated approach
isSafeInteger(42); // => true
isSafeInteger(9007199254740991); // => true (MAX_SAFE_INTEGER)
isSafeInteger(9007199254740992); // => false

// ✅ Recommended approach
Number.isSafeInteger(42); // => true
Number.isSafeInteger(9007199254740991); // => true
Number.isSafeInteger(9007199254740992); // => false

How it works?

Checks if value is a safe integer (exactly representable in IEEE-754).

Safe Integer Range

Common Checks

ValueResult
42 true
9007199254740991 true (MAX_SAFE_INTEGER)
9007199254740992 false
3.14 false
Infinity false

Deprecated: Use Number.isSafeInteger() directly.


Use Cases

Validate safe integer 📌

Check if value is a safe integer (within IEEE-754 precision).

Number.isSafeInteger(42);                 // true
Number.isSafeInteger(9007199254740991); // true (MAX_SAFE_INTEGER)
Number.isSafeInteger(9007199254740992); // false

Guard ID operations

Ensure IDs don't lose precision.

function processId(id: number) {
if (!Number.isSafeInteger(id)) {
throw new Error('ID too large, use BigInt');
}
return fetchRecord(id);
}