Skip to content

unnecessaryTernaries

Reports ternary expressions that can be simplified to boolean expressions or logical operators

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

Ternary expressions can sometimes be simplified to more concise and readable forms. In particular:

  1. Boolean ternaries: ternary expressions that return true or false based on a condition can be simplified to the condition itself or its negation.
  2. Redundant ternaries: ternary expressions where the consequent duplicates the condition can be replaced with a logical OR operator (||).

This rule detects and reports ternary expressions that can be replaced with boolean expressions or logical operators.

AEconsB0isActiAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53statAE ==<= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dacti1<1"sp ansty="le--0:#C792EA;--1:#8844">AE 48trAE <: sp ansty="le--0:#FF6A83;--1:#A248">48fal5<3;d ivclassNa="meco">AEconsB0isInactiAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53statAE ==<= sp ansty="le--0:#D9F5DD;--1:#1111">1<1"sp ansty="le--0:#ECC48D;--1:#984E">4Dacti1<1"sp ansty="le--0:#C792EA;--1:#8844">AE 48falAE <: sp ansty="le--0:#FF6A83;--1:#A248">48tr5<3;d ivclassNa="meco">AEconsB0resuAE <= sp ansty="le--0:#D6DEEB;--1:#403F">53valAE 53valAE <: sp ansty="le--0:#D6DEEB;--1:#403F">53defaultValuAEconsB0resuAE =< !sp ansty="le--0:#D6DEEB;--1:#403F">53valAE 53alternatiAE <: sp ansty="le--0:#D6DEEB;--1:#403F">53valu
44AEco61B0isAc44A3F53st44AE <=== spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dac1<111" spans="tyle--0:#D6DEEB;--1:#40">3<;di vdivclass="Nameexpressive-">44AEco61B0isInac44AE< = ! spans="tyle--0:#D6DEEB;--1:#40">3F53(st44AE <=== spans="tyle--0:#D9F5DD;--1:#11">1<111" spans="tyle--0:#ECC48D;--1:#98">4E4Dac1<111" spans="tyle--0:#D6DEEB;--1:#40">3F<53) ;divclass="Name"><;di vdivclass="Nameexpressive-">44AEco61B0re44A3F53v44AE< || spans="tyle--0:#D6DEEB;--1:#40">3F53defaultVa<;di vdivclass="Nameexpressive-">44AEco61B0re44A3F53v44AE< || spans="tyle--0:#D6DEEB;--1:#40">3F53alternat

This rule is not configurable.

If you have a large codebase with established patterns using ternary expressions for clarity, or if your team prefers explicit ternary expressions over logical operators, this rule might not be for you.