Linter · Rules
unicorn/prefer-number-properties
Require Number.isNaN, Number.parseInt and the like instead of the globals
The global isNaN and isFinite convert their argument to a number first, so isNaN("abc") is true and isFinite("1") is true. Number.isNaN and Number.isFinite don't convert, so they're false for anything that isn't a number.
Number.parseInt, Number.parseFloat and Number.NaN are the same as the globals. The rule reports the globals so that everything about numbers is written the same way.
--fix rewrites parseInt, parseFloat, NaN and Infinity. It doesn't touch isNaN and isFinite, because the result can change. --fix-dangerously rewrites those too.
Configure
Off by default. To turn it on:
[lint.rules]
"unicorn/prefer-number-properties" = "error" # or "warn"
Every option, set to its default:
[lint.rules]
"unicorn/prefer-number-properties" = ["error", {
checkInfinity = false,
checkNaN = true,
}]
Examples
Reported:
const width = parseInt(style.width, 10);
if (isNaN(width)) {
reset(NaN);
}
const isValid = window.isFinite(width);
Not reported:
const width = Number.parseInt(style.width, 10);
if (Number.isNaN(width)) {
reset(Number.NaN);
}
const isValid = Number.isFinite(width);
Options
| Option | Default | What it does |
|---|---|---|
checkInfinity | false | Also report Infinity and -Infinity. Write Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY. |
checkNaN | true | Report NaN. |
Infinity is only reported with checkInfinity:
let lowest = Infinity;
let lowest = Infinity;
With checkNaN: false, NaN is allowed:
const missing = NaN;