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.

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
;

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.