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#
const width = parseIntUse Number.parseInt instead of the global parseInt(style.width, 10);
if (isNaNUse Number.isNaN instead of the global isNaN(width)) {
reset(NaNUse Number.NaN instead of the global NaN);
}
const isValid = window.isFiniteUse Number.isFinite instead of the global isFinite(width);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;{ "checkInfinity": true }1 problemlet lowest = InfinityUse Number.Infinity instead of the global Infinity;With checkNaN: false, NaN is allowed:
{ "checkNaN": false }No problemsconst missing = NaN;