Skip to content
← Back to rules

eslint/prefer-numeric-literals Style

🛠️ An auto-fix is available for this rule for some violations.

它的作用

禁止使用 parseInt()Number.parseInt(),而应使用二进制、八进制和十六进制 字面量。

为什么这不好?

parseInt()Number.parseInt() 函数可用于将二进制、八进制和 十六进制字符串转换为整数。由于二进制、八进制和十六进制字面量已在 ES2015 中得到支持, 此规则鼓励使用这些数值字面量,而不是 parseInt()Number.parseInt()

示例

以下是此规则的错误代码示例:

javascript
parseInt("111110111", 2) === 503;
parseInt(`111110111`, 2) === 503;
parseInt("767", 8) === 503;
parseInt("1F7", 16) === 503;
Number.parseInt("111110111", 2) === 503;
Number.parseInt("767", 8) === 503;
Number.parseInt("1F7", 16) === 503;

如何使用

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

json
{
  "rules": {
    "prefer-numeric-literals": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "prefer-numeric-literals": "error",
  },
});
bash
oxlint --deny prefer-numeric-literals

版本

此规则添加于 v0.7.0。

参考资料