Skip to content

noTernary

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noTernary": "error"
}
}
}
}

¥Description

禁止使用三元运算符。

¥Disallow ternary operators.

三元运算符用于有条件地为变量赋值。有些人认为使用三元运算符会导致代码不清晰。

¥The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.

¥Examples

¥Invalid

const foo = isBar ? baz : qux;
code-block.js:1:13 lint/nursery/noTernary ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected ternary operator.

> 1 │ const foo = isBar ? baz : qux;
^^^^^^^^^^^^^^^^^
2 │

Ternary operators can lead to unclear code. Use if-else statement instead.

¥Valid

let foo;
if (isBar) {
foo = baz;
} else {
foo = qux;
}

¥Related links