Skip to content

builtinConstructorNews

Enforces using new for constructors that require it, and disallows new for primitive coercion functions.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

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:

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.

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) ― Value of type 'MapConstructor' is not callable. Did you mean to include 'new'?
const
const str: String
str
= new
var String: StringConstructor
new (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: NumberConstructor
new (value?: any) => Number

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
(42);

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.