Skip to main content

prefer-ts-extras-is-safe-integer

Prefer isSafeInteger from ts-extras over Number.isSafeInteger(...).

This keeps predicate usage consistent with other ts-extras narrowing helpers.

Targeted pattern scopeโ€‹

This rule focuses on direct Number.isSafeInteger(value) calls that can be migrated to isSafeInteger(value) with deterministic fixes.

  • Number.isSafeInteger(value) call sites that can use isSafeInteger(value).

Alias indirection, wrapper helpers, and non-canonical call shapes are excluded to keep isSafeInteger(value) migrations safe.

What this rule reportsโ€‹

This rule reports Number.isSafeInteger(value) call sites when isSafeInteger(value) is the intended replacement.

  • Number.isSafeInteger(value) call sites that can use isSafeInteger(value).

Why this rule existsโ€‹

isSafeInteger standardizes safe-integer validation and keeps numeric guard usage consistent with other ts-extras helpers.

  • Safety-bound checks use one helper convention.
  • Predicate style is consistent with isInteger and isFinite.
  • Guard code for IDs/counters avoids mixed native/helper forms.

โŒ Incorrectโ€‹

const isSafe = Number.isSafeInteger(value);

โœ… Correctโ€‹

const isSafe = isSafeInteger(value);

Behavior and migration notesโ€‹

  • Runtime behavior matches native Number.isSafeInteger.
  • Values outside Number.MIN_SAFE_INTEGER / Number.MAX_SAFE_INTEGER return false.
  • Non-number values are not coerced.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

if (Number.isSafeInteger(quantity)) {
persist(quantity);
}

โœ… Correct โ€” Additional exampleโ€‹

if (isSafeInteger(quantity)) {
persist(quantity);
}

โœ… Correct โ€” Repository-wide usageโ€‹

const supported = isSafeInteger(index);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-is-safe-integer": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your team enforces native Number.isSafeInteger calls.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/is-safe-integer.ts

/**
A strongly-typed version of `Number.isSafeInteger()`.

@category Improved builtin
@category Type guard
*/

Rule catalog ID: R023

Further readingโ€‹

Adoption resourcesโ€‹