Skip to content

newExpressions

Reports standalone new expressions that don't use the constructed object.

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

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.

76// Constructing an object without using 72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0MyCla53(<);d ivclassNa="meec-li">dAEfuncti5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0initiali11<()sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#919F9F;--1:#5D63">76// Creating an instance but not capturing nt< sp ansty="le--0:#7FDBCA;--1:#096E">72n5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0EventEmitt53(<);d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
6376// Store the constructed ob44AEco61B0inst44A6E7<2new spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#82AAFF;--1:#3B">61B0MyC3F5<3() ;divclass="Nameec-">c44AEfunc361B0initia11<11() spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">de6376// Use the constructed obde44AEco61B0emi44A6E7<2new spans="tyle--0:#C792EA;--1:#88">4<4AE spans="tyle--0:#82AAFF;--1:#3B">61B0EventEmi3F5<3() ;divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">3F53emi4<4AE. spans="tyle--0:#82AAFF;--1:#3B">6131<111" spans="tyle--0:#ECC48D;--1:#98">4E4D1<111" spans="tyle--0:#889FB2;--1:#4D">6<67B, spans="tyle--0:#D6DEEB;--1:#40">3F53 handleDa3c6376// Return the constructed ob44AEfunc361B0createInst11<11() spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">de44AEre36E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0MyC3F5<3() ;divclass="Nameec-">3c6376// Throw an error ob44AEt36E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0E31<111" spans="tyle--0:#ECC48D;--1:#98">4E4DSomething went w1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name">

This rule is not configurable.

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.