close

no-unneeded-ternary

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-unneeded-ternary': 'error',
    },
  },
]);

Rule Details

Disallows ternary expressions when a simpler alternative exists. Two patterns are flagged:

  • Boolean-literal selectioncond ? true : false (or any combination of boolean literals on both arms) collapses to cond, !cond, !!cond, or the boolean literal itself. Always reported.
  • Default assignmenta ? a : b is equivalent to a || b. Reported only when the defaultAssignment option is set to false.

Examples of incorrect code for this rule:

var a = x === 2 ? true : false;
var b = x ? true : false;

Examples of correct code for this rule:

var a = x === 2 ? "Yes" : "No";
var b = x !== false;
var c = x ? "Yes" : "No";
var d = x ? y : x;

Examples of incorrect code for this rule with { "defaultAssignment": false }:

{ "no-unneeded-ternary": ["error", { "defaultAssignment": false }] }
var a = x ? x : 1;
f(x ? x : 1);

Options

OptionTypeDefaultDescription
defaultAssignmentbooleantrueWhen false, also flag the a ? a : b default-assignment pattern (auto-fixed to a || b).

Original Documentation

no-unneeded-ternary - ESLint