Skip to content

asConstAssertions

Reports using explicit literal types when as const can be used.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

There are two common ways to tell TypeScript that a literal value should be interpreted as its literal type rather than a general primitive type:

  • as const: telling TypeScript to infer the literal type automatically
  • as with an explicit literal type: explicitly telling TypeScript the literal type

as const is generally preferred as it doesn’t require repeating the literal value. This rule reports when an explicit literal type can be replaced with as const.

AEle53val7<2:sp ansty="le--0:#C792EA;--1:#8844">A8<22sp ansty="le--0:#C792EA;--1:#8844">AE <= sp ansty="le--0:#F78C6C;--1:#AA09">8<22sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco"><2;d>AEle53valAE <= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dhel1<1"sp ansty="le--0:#C792EA;--1:#8844">AE a1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dhel1<1"sp ansty="le--0:#D6DEEB;--1:#403F">5<3;d ivclassNa="meco"><";d>AEconsB0fl7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0trAE <= sp ansty="le--0:#FF6A83;--1:#A248">48tr5<3;d ivclassNa="meco">
44AE3F53v44A0<9822 spans="tyle--0:#C792EA;--1:#88">44AE as c3<;di vdivclass="Nameexpressive-">44AE3F53v44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dh1<111" spans="tyle--0:#C792EA;--1:#88">44AE as c3<;di vdivclass="Nameexpressive-">44AE3F53v44A1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dh1<111" spans="tyle--0:#C792EA;--1:#88">44AE< as spans="tyle--0:#C5E478;--1:#3B">61B0st3<;di vdivclass="Nameexpressive-">44AEco61B044A484844AE as c3

This rule is not configurable.

If your codebase consistently uses explicit literal types for documentation purposes, or if you don’t have a preference between the two styles, you may disable this rule.