Skip to content
← Back to rules

unicorn/prefer-bigint-literals Style

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

它的作用

要求使用 BigInt 字面量(例如 123n),而不是调用 BigInt() 构造函数 并传入字面量参数,例如数字或数字字符串。

为什么这不好?

对字面量值使用 BigInt(…) 过于冗长,也不如使用 BigInt 字面量符合习惯。

示例

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

js
BigInt(0);
BigInt(123);
BigInt(0xff);
BigInt(1e3);
BigInt("42");
BigInt("0x10");

以下是此规则的正确代码示例:

js
0n;
123n;
0xffn;
1000n;
// 非整数、动态或非字面量输入:
BigInt(x);
BigInt("not-a-number");
BigInt("1.23");

如何使用

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

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

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

版本

此规则是在 v1.30.0 中添加的。

参考资料