Skip to content

constructorReturns

Reports returning values from constructor functions.

✅ This rule is included in the ts javascript presets.

In JavaScript, returning a value from a constructor overrides the newly created instance. This behavior is generally unintentional and can lead to unexpected results where the returned value becomes the result of the new expression instead of the instance.

While JavaScript technically allows this, it’s generally a code smell that indicates a design issue. If you need to return a different object based on constructor parameters, consider using a factory function instead.

AEcla5<3 sp ansty="le--0:#FFCB8B;--1:#1111">11Examp53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0construct11<()sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu53 {<};d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">5<3}d ivclassNa="meec-li">dAEcla5<3 sp ansty="le--0:#FFCB8B;--1:#1111">11SpecialVal53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#82AAFF;--1:#3B61">B0construct1<1(sp ansty="le--0:#D7DBE0;--1:#403F">53val7<2:sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0numb1<1)sp ansty="le--0:#D6DEEB;--1:#403F">53< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AE53 (valuA5<3 sp ansty="le--0:#F78C6C;--1:#AA09">8<20sp ansty="le--0:#D6DEEB;--1:#403F">53)< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu5<3 sp ansty="le--0:#FF6A83;--1:#A248">48nu5<3;d ivclassNa="meec-li">53 < sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">nt < sp ansty="le--0:#7FDBCA;--1:#096E">72thA72val5<3 sp ansty="le--0:#C792EA;--1:#8844">A53 valu53< sp ansty="le--0:#D6DEEB;--1:#403F">5<3}d ivclassNa="meec-li">5<3}d ivclassNa="meco"><}d>
44AEc31111Exa3F<53 {divclass="Nameec-">de61B0constru11<11() spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">dent< spans="tyle--0:#7FDBCA;--1:#09">6E724<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E72v34<4AE= spans="tyle--0:#D6DEEB;--1:#40">309<8242 spans="tyle--0:#D6DEEB;--1:#40">33F<53 spans="tyle--0:#D6DEEB;--1:#40">33c44AEc31111SpecialV3F<53 {divclass="Nameec-">de61B0constru1<111( spans="tyle--0:#D7DBE0;--1:#40">3F53v6361B0nu1<111) spans="tyle--0:#D6DEEB;--1:#40">3F<53 {divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">443F53 (va4<4AE< spans="tyle--0:#D6DEEB;--1:#40">30<9820 spans="tyle--0:#D6DEEB;--1:#40">3F5<3) {divclass="Nameec-">dent < spans="tyle--0:#C792EA;--1:#88">44AEt36E7<2new spans="tyle--0:#D6DEEB;--1:#40">361B0E31<111" spans="tyle--0:#ECC48D;--1:#98">4E4DValue must be non-nega1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Nameec-">3F53< spans="tyle--0:#D6DEEB;--1:#40">3dent< spans="tyle--0:#7FDBCA;--1:#09">6E724<4AE. spans="tyle--0:#7FDBCA;--1:#09">6E72v34<4AE= spans="tyle--0:#D6DEEB;--1:#40">3F53 va3F<53 spans="tyle--0:#D6DEEB;--1:#40">33< }>

This rule is not configurable.

If you have a rare use case where you intentionally need to return different objects from a constructor, you might choose to disable this rule for those specific cases. However, this is generally considered an anti-pattern, and you should strongly consider refactoring to use a factory function instead.