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 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:

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 list = Array(10);
const now = Date();
const map = Map([["key", "value"]]);
const str = new String("hello");
const num = new 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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.