newExpressions
Reports standalone new expressions that don't use the constructed object.
✅ This rule is included in the ts logical
preset.
Using the new
keyword to construct an object without storing or using the result is often a mistake or indicates unclear code intent.
If a constructor has side effects that you want to trigger, it’s better to make those side effects explicit by calling a separate method rather than relying on constructor side effects.
Examples
Section titled “Examples”// Constructing an object without using itnew MyClass();
function initialize() { // Creating an instance but not capturing it new EventEmitter();}
// Store the constructed objectconst instance = new MyClass();
function initialize() { // Use the constructed object const emitter = new EventEmitter(); emitter.on("data", handleData);}
// Return the constructed objectfunction createInstance() { return new MyClass();}
// Throw an error objectthrow new Error("Something went wrong");
Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you have constructors that are intentionally designed to have side effects and you want to call them without using their return value, you might choose to disable this rule. However, in most cases, it’s better to refactor such code to make the side effects explicit through regular function calls rather than relying on constructor side effects.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
no-new
- Oxlint:
eslint/no-new
oxc/missing-throw