Using the new
operator with Symbol
and BigInt
will throw a TypeError
because they are not
intended to be used as constructors. Symbol
and BigInt
are primitive types in JavaScript and should be used as such.
This is different from the other primitive types, such as string, number or boolean, where it was possible to call global String
or
Number
as functions that return primitive types, but also use them as constructors with the new
operator to create wrapper
objects. This confusing double behavior is not implemented for Symbol
and BigInt
types that were introduced later in the
language.
This behavior would be especially problematic for symbols that have reference identity and already behave like objects in some way. For example,
they are garbage collectable and therefore can be used as keys in WeakMap and WeakSet objects.
let foo = new Symbol('abc'); // Noncompliant: TypeError: Symbol is not a constructor
let bar = new BigInt(123); // Noncompliant: TypeError: BigInt is not a constructor
To fix the code remove the new
operator.
let foo = Symbol('abc');
let bar = BigInt(123);
For the BigInt
type to be recognized correctly, the environment should be es2020
or higher.