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
const primitiveString: "hello"
primitiveString
= "hello";
const
const wrappedString: String
wrappedString
= 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 primitiveString: "hello"
primitiveString
=== "hello"; // true
const wrappedString: String
wrappedString
=== "hello"; // false - object vs primitive comparison
typeof
const primitiveString: "hello"
primitiveString
; // "string"
typeof
const wrappedString: String
wrappedString
; // "object"

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

const
const text: String
text
= 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 count: Number
count
= 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);
const
const flag: Boolean
flag
= new
var Boolean: BooleanConstructor
new (value?: any) => Boolean
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 ❤️‍🔥 around the world by the Flint team and contributors.