Skip to main content

prefer-ts-extras-is-infinite

Require isInfinite from ts-extras over direct Infinity equality checks.

Targeted pattern scopeโ€‹

This rule only matches direct equality checks against infinity constants that can be collapsed into isInfinite(value).

  • Direct infinity equality checks:
  • value === Infinity
  • value === Number.POSITIVE_INFINITY
  • value === Number.NEGATIVE_INFINITY

Syntactically similar alternatives are intentionally out of scope unless they preserve the same AST shape.

What this rule reportsโ€‹

This rule reports direct infinity equality checks that can be replaced with isInfinite(value).

  • Direct infinity equality checks:
  • value === Infinity
  • value === Number.POSITIVE_INFINITY
  • value === Number.NEGATIVE_INFINITY

Why this rule existsโ€‹

isInfinite replaces constant-based comparisons with one explicit predicate.

  • Infinity checks follow one helper pattern.
  • Mixed positive/negative infinity comparisons are normalized.
  • Numeric guard code is easier to audit.

โŒ Incorrectโ€‹

const infinite = value === Infinity || value === Number.NEGATIVE_INFINITY;

โœ… Correctโ€‹

const infinite = isInfinite(value);

Behavior and migration notesโ€‹

  • isInfinite(value) covers both Infinity and -Infinity.
  • Finite numbers and NaN return false.
  • This rule targets direct equality checks, not broader numeric validation chains.

Additional examplesโ€‹

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

const bad = value === Infinity || value === Number.NEGATIVE_INFINITY;

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

const bad = isInfinite(value);

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

if (isInfinite(rate)) {
throw new Error("invalid rate");
}

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

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

When not to use itโ€‹

Disable this rule if direct infinity constant comparisons are required in generated code.

Package documentationโ€‹

ts-extras package documentation:

Source file: source/is-infinite.ts

/**
Check whether a value is infinite.

@example
```
import {isInfinite} from 'ts-extras';

isInfinite(Number.POSITIVE_INFINITY);
//=> true

isInfinite(Number.NEGATIVE_INFINITY);
//=> true

isInfinite(42);
//=> false

isInfinite(Number.NaN);
//=> false
```

@category Type guard
*/

Rule catalog ID: R019

Further readingโ€‹

Adoption resourcesโ€‹