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