Skip to content
← Back to rules

unicorn/prefer-number-properties Restriction

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

作用

禁止将 parseInt()parseFloat()isNan()isFinite()NanInfinity-Infinity 作为全局变量使用。

为什么这不好?

ECMAScript 2015 为了保持一致性并稍微改进这些全局对象,将它们移到了 Number 构造函数上。此规则强制使用这些属性,以减少全局变量的使用:

示例

此规则的错误代码示例:

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

此规则的正确代码示例:

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

配置

此规则接受一个包含以下属性的配置对象:

checkInfinity

type: boolean

default: false

如果设置为 true,则检查是否将 Infinity-Infinity 作为全局变量使用。

checkNaN

type: boolean

default: true

如果设置为 true,则检查是否将 NaN 作为全局变量使用。

如何使用

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

版本

此规则在 v0.0.19 中加入。

参考资料