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 === Infinityvalue === Number.POSITIVE_INFINITYvalue === 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 === Infinityvalue === Number.POSITIVE_INFINITYvalue === 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 bothInfinityand-Infinity.- Finite numbers and
NaNreturnfalse. - 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