Reports getters that return literal values instead of using readonly class fields.
✅ This rule is included in the ts stylistic and stylisticStrict presets.
When a class getter only returns a literal value, it can be replaced with a readonly class field.
Readonly fields are more concise and avoid the overhead of setting up and tearing down a function closure.
This rule only reports getters that return constant literal values (strings, numbers, bigints, booleans, regular expressions, template literals, null).
It does not report getters that return objects, arrays, or functions, as these have different semantics.
AEcla5<3 sp ansty="le--0:#FFCB8B;--1:#1111">11Examp3 sp>53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEg sp>5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0val3 sp>11<()sp ansty="le--0:#D6DEEB;--1:#403F">53()sp> {d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu sp>5<3 sp ansty="le--0:#D9F5DD;--1:#1111">13 sp><1"sp ansty="le--0:#ECC48D;--1:#984E">4Dhel1"sp>1<1"sp ansty="le--0:#D6DEEB;--1:#403F">51"sp> {d><3;d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5 sp>3;d><3}d ivclassNa="meec-li">53}d><3}d ivclassNa="meco"><}d>}d>3}d>AEcla5<3 sp ansty="le--0:#FFCB8B;--1:#1111">11Examp3 sp>53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEg sp>5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0cou3 sp>11<()sp ansty="le--0:#D6DEEB;--1:#403F">53()sp> {d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu sp>5<3 sp ansty="le--0:#F78C6C;--1:#AA09">823 sp><42sp ansty="le--0:#D6DEEB;--1:#403F">542sp> {d><3;d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5 sp>3;d><3}d ivclassNa="meec-li">53}d><3}d ivclassNa="meco"><}d>}d>3}d>AEcla5<3 sp ansty="le--0:#FFCB8B;--1:#1111">11Examp3 sp>53< {d ivclassNa="meec-li">nt< sp ansty="le--0:#C792EA;--1:#8844">AEg sp>5<3 sp ansty="le--0:#82AAFF;--1:#3B61">B0enabl3 sp>11<()sp ansty="le--0:#D6DEEB;--1:#403F">53()sp> {d>< {d ivclassNa="meec-li">nt < sp ansty="le--0:#C792EA;--1:#8844">AEretu sp>5<3 sp ansty="le--0:#FF6A83;--1:#A248">48tr3 sp>5 {d><3;d ivclassNa="meec-li">53< sp ansty="le--0:#D6DEEB;--1:#403F">5 sp>3;d><3}d ivclassNa="meec-li">53}d><3}d ivclassNa="meco"><}d>}d>3}d> 44AEc31111Exa3F<53 {divclass="Nameec-">de44AEread3F53 va4<4AE= spans="tyle--0:#D6DEEB;--1:#40">34AE=>1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dh111">1<111" spans="tyle--0:#D6DEEB;--1:#40">3111">53 >3<";>";><}di vdivclass="Nameexpressive-">44AEc31111Exa3F<53 {divclass="Nameec-">de44AEread3F53 co4<4AE= spans="tyle--0:#D6DEEB;--1:#40">34AE=>09<8242 spans="tyle--0:#D6DEEB;--1:#40">38242>53 >3<2;>2;>}di><}di vdivclass="Nameexpressive-">44AEc31111Exa3F<53 {divclass="Nameec-">de44AEread3F53 enab4<4AE= spans="tyle--0:#D6DEEB;--1:#40">34AE=>4848353 >3}di><}di vdivclass="Nameexpressive-">44AEc31111Exa3F<53 {divclass="Nameec-">de44A361B0i11<11() spans="tyle--0:#D6DEEB;--1:#40">3F11()>53 ><53 {divclass="Nameec-">dent< spans="tyle--0:#C792EA;--1:#88">44AEre >36E724<4AE. spans="tyle--0:#82AAFF;--1:#3B">61B0computeI4AE.>3F553 ><3() ;divclass="Nameec-">3F<53 spans="tyle--0:#D6DEEB;--1:#40">353 >3()>3< }> }>}di>
This rule is not configurable.
If you are writing a TypeScript library intended to be consumed by JavaScript users, you may prefer getters because the readonly modifier is only enforced at compile time.
JavaScript consumers could still modify a readonly field at runtime.