Skip to content
← Back to rules

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 using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/prefer-number-properties": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/prefer-number-properties": "error",
  },
});
bash
oxlint --deny unicorn/prefer-number-properties

Version ​

This rule was added in v0.0.19.

References ​