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 const isActive: boolean
isActive = const status: any
status === "active" ? true : false;const const isInactive: boolean
isInactive = const status: any
status === "active" ? false : true;const const result: any
result = const value: any
value ? const value: any
value : const defaultValue: any
defaultValue;const const result: any
result = !const value: any
value ? const alternative: any
alternative : const value: any
value;const const isActive: boolean
isActive = const status: any
status === "active";const const isInactive: boolean
isInactive = !(const status: any
status === "active");const const result: any
result = const value: any
value || const defaultValue: any
defaultValue;const const result: any
result = const value: any
value || const alternative: any
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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 around the world by
the Flint team and contributors.