Skip to content
← Back to rules

eslint/no-new-native-nonconstructor 正确性

This rule is turned on by default.

作用

禁止对全局的非构造函数(SymbolBigInt)使用 new 运算符。

对于 TypeScript 代码,可以禁用此规则,因为 TypeScript 编译器会强制执行此检查。

为什么这不好?

new Symbolnew BigInt 都会抛出类型错误,因为它们是函数而不是类。很容易因为大写字母而误以为它们是类,从而犯下这个错误。

示例

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

js
// 抛出 TypeError
let foo = new Symbol("foo");

// 抛出 TypeError
let result = new BigInt(9007199254740991);

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

js
let foo = Symbol("foo");

let result = BigInt(9007199254740991);

如何使用

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

json
{
  "rules": {
    "no-new-native-nonconstructor": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-new-native-nonconstructor": "error",
  },
});
bash
oxlint --deny no-new-native-nonconstructor

版本

此规则添加于 v0.3.3。

参考资料