π Prefer using a logical operator over a ternary.
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π‘ This rule is manually fixable by editor suggestions.
Disallow ternary operators when simpler logical operator alternatives exist.
Ideally, most reported cases have an equivalent Logical OR(||) expression. The rule intentionally provides suggestions instead of auto-fixes, because in many cases, the nullish coalescing operator (??) should be preferred.
// β
foo ? foo : bar;
// β
foo ?? bar;
// β
foo || bar;// β
foo.bar ? foo.bar : foo.baz
// β
foo.bar ?? foo.baz// β
foo?.bar ? foo.bar : baz
// β
foo?.bar ?? baz// β
!bar ? foo : bar;
// β
bar ?? foo;// β
foo ? bar : baz;