builtinConstructorNews
Enforces using new for constructors that require it, and disallows new for primitive coercion functions.
✅ This rule is included in the ts stylistic preset.
Many built-in JavaScript objects have tricky behaviors around the new keyword.
Some only work as expected when called with new, while others cannot or should not be called with new.
This rule enforces consistent and safe calling of built-in JavaScript constructors using or not using new.
Specifically, this rule enforces using new for following built-in constructors:
- Arrays and array-likes:
Array,ArrayBuffer,SharedArrayBuffer,BigInt64Array,BigUint64Array,Float16Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint16Array,Uint32Array,Uint8ClampedArray - Common objects:
Date,Error,Function,Object,Promise,RegExp - Common data structures:
Map,WeakMap,Set,WeakSet - Other objects:
DataView,Proxy,WeakRef,FinalizationRegistry
This rule disallows using new for following built-in constructors:
Using new with primitive coercion functions creates object wrappers that can cause unexpected behavior in comparisons and type checks.
Examples
Section titled “Examples”const list = Array(10);const now = Date();const map = Map([["key", "value"]]);const str = new String("hello");const num = new Number(42);const list = new Array(10);const now = new Date();const map = new Map([["key", "value"]]);const str = String(value);const num = Number(value);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally use object wrappers for primitives (e.g., new String()) for specific purposes like adding properties to a string value, you may disable this rule.
For example, if you intentionally manage JavaScript primitives in a legacy codebase that does not adhere to common modern conventions, this rule may be counterproductive for you.
Further Reading
Section titled “Further Reading”- MDN documentation on Array constructor
- MDN documentation on Map constructor
- MDN documentation on String primitives and String objects
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
noInvalidBuiltinInstantiation - ESLint:
unicorn/new-for-builtins - Oxlint:
unicorn/new-for-builtins