no-unneeded-ternary

Disallow ? : where the condition alone says the same

fixable

age >= 18 ? true : false is a long way to write age >= 18.

--fix rewrites the expression: a === b ? true : false becomes a === b, user ? true : false becomes !!user, and user ? false : true becomes !user.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-unneeded-ternary" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-unneeded-ternary" = ["error", {
  defaultAssignment = true,
}]

Examples#

example.js2 problems
const isAdult = age >= 18 ? true : false;
const isGuest = user ? false : true;
example.jsNo problems
const isAdult = age >= 18;
const isGuest = !user;
const label = isAdult ? "adult" : "minor";

Options#

OptionDefaultWhat it does
defaultAssignmenttrueAllow x ? x : fallback. Set it to false to report that too.
example.jsNo problems
const title = name ? name : "Untitled";
example.jswith { "defaultAssignment": false }1 problem
const title = name ? name : "Untitled";

With false, --fix rewrites it with ||:

example.jswith { "defaultAssignment": false }No problems
const title = name || "Untitled";