Skip to content

wrapperObjects

Flags using new with Boolean, Number, or String, which creates wrapper objects instead of primitives.

✅ This rule is included in the ts logical and logicalStrict presets.

Using new with Boolean, Number, or String creates object wrappers around primitive values. These wrapper objects behave differently from primitives in surprising ways:

const primitiveString = "hello";
const wrappedString = new String("hello");
primitiveString === "hello"; // true
wrappedString === "hello"; // false - object vs primitive comparison
typeof primitiveString; // "string"
typeof wrappedString; // "object"

Wrapper objects are almost never intended. This rule reports when a wrapper object is created instead of a normal primitive.

const text = new String("hello");
const count = new Number(42);
const flag = new Boolean(true);

This rule is not configurable.

If you intentionally use object wrappers for primitives, for example to attach custom properties to a value, you may disable this rule. However, this pattern is extremely rare in modern JavaScript and TypeScript codebases.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.