builtinConstructorNews
Enforces using new for constructors that require it, and disallows new for primitive coercion functions.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
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 const list: any[]
list = var Array: ArrayConstructor(arrayLength?: number) => any[] (+2 overloads)
Array(10);const const now: string
now = var Date: DateConstructor() => string
Enables basic storage and retrieval of dates and times.
Date();const const map: any
map = var Map: MapConstructor
Map([["key", "value"]]);Error ts(2348) ― const const str: String
str = new var String: StringConstructornew (value?: any) => String
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String("hello");const const num: Number
num = new var Number: NumberConstructornew (value?: any) => Number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number(42);const const list: any[]
list = new var Array: ArrayConstructornew (arrayLength?: number) => any[] (+2 overloads)
Array(10);const const now: Date
now = new var Date: DateConstructornew () => Date (+3 overloads)
Date();const const map: Map<string, string>
map = new var Map: MapConstructornew <string, string>(iterable?: Iterable<readonly [string, string]> | null | undefined) => Map<string, string> (+3 overloads)
Map([["key", "value"]]);const const str: string
str = var String: StringConstructor(value?: any) => string
Allows manipulation and formatting of text strings and determination and location of substrings within strings.
String(const value: any
value);const const num: number
num = var Number: NumberConstructor(value?: any) => number
An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.
Number(const value: any
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