unnecessaryTernaries
Reports ternary expressions that can be simplified to boolean expressions or logical operators
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
Ternary expressions can sometimes be simplified to more concise and readable forms. In particular:
- Boolean ternaries: ternary expressions that return
trueorfalsebased on a condition can be simplified to the condition itself or its negation. - 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.
Examples
Section titled “Examples”const isActive = status === "active" ? true : false;const isInactive = status === "active" ? false : true;const result = value ? value : defaultValue;const result = !value ? alternative : value;const isActive = status === "active";const isInactive = !(status === "active");const result = value || defaultValue;const result = value || alternative;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.