Skip to content

unicorn/prefer-number-properties Restriction

⚠️🛠️️ A dangerous auto-fix is available for this rule.

What it does

Disallows use of parseInt(), parseFloat(), isNan(), isFinite(), Nan, Infinity and -Infinity as global variables.

Why is this bad?

ECMAScript 2015 moved globals onto the Number constructor for consistency and to slightly improve them. This rule enforces their usage to limit the usage of globals:

Examples

Examples of incorrect code for this rule:

javascript
const foo = parseInt("10", 2);
const bar = parseFloat("10.5");

Examples of correct code for this rule:

javascript
const foo = Number.parseInt("10", 2);
const bar = Number.parseFloat("10.5");

Configuration

This rule accepts a configuration object with the following properties:

checkInfinity

type: boolean

default: false

If set to true, checks for usage of Infinity and -Infinity as global variables.

checkNan

type: boolean

default: true

If set to true, checks for usage of NaN as a global variable.

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-number-properties
json
{
  "rules": {
    "unicorn/prefer-number-properties": "error"
  }
}

References

Released under the MIT License.