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 : falseUnnecessary use of boolean literals in conditional expression.;
const isGuest = user ? false : trueUnnecessary use of boolean literals in conditional expression.;example.jsNo problems
const isAdult = age >= 18;
const isGuest = !user;
const label = isAdult ? "adult" : "minor";Options#
| Option | Default | What it does |
|---|---|---|
defaultAssignment | true | Allow x ? x : fallback. Set it to false to report that too. |
example.jsNo problems
const title = name ? name : "Untitled";example.jswith
{ "defaultAssignment": false }1 problemconst title = name ? name : "Untitled"Unnecessary use of conditional expression for default assignment.;With false, --fix rewrites it with ||:
example.jswith
{ "defaultAssignment": false }No problemsconst title = name || "Untitled";