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: string
status === "active" ? true : false;const const isInactive: boolean
isInactive = const status: string
status === "active" ? false : true;const const result: unknown
result = const value: unknown
value ? const value: {}
value : const defaultValue: unknown
defaultValue;const const result: unknown
result = !const value: unknown
value ? const alternative: unknown
alternative : const value: {}
value;const const isActive: boolean
isActive = const status: string
status === "active";const const isInactive: boolean
isInactive = !(const status: string
status === "active");const const result: unknown
result = const value: unknown
value || const defaultValue: unknown
defaultValue;const const result: unknown
result = const value: unknown
value || const alternative: unknown
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.