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 isActive = status === "active" ? true : false;
const isInactive = status === "active" ? false : true;
const result = value ? value : defaultValue;
const result = !value ? alternative : 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.