wrapperObjects
Flags using
newwithBoolean,Number, orString, which creates wrapper objects instead of primitives.
✅ This rule is included in the ts logical 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"; // truewrappedString === "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.
Examples
Section titled “Examples”const text = new String("hello");const count = new Number(42);const flag = new Boolean(true);const text = String(value);const count = Number(value);const flag = Boolean(value);const text = "hello";const count = 42;const flag = true;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”- MDN documentation on String primitives and String objects
- MDN documentation on Number
- MDN documentation on Boolean
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- Biome:
useConsistentBuiltinInstantiation - ESLint:
no-new-wrappers - Oxlint:
eslint/no-new-wrappers
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.