Перейти к содержимому

noUnnecessaryConditions

Это содержимое пока не доступно на вашем языке.

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

Disallow unnecessary type-based conditions that can be statically determined as redundant.

This rule detects if expressions inside conditions are statically inferrable and yield falsy or truthy values that don’t change during the life cycle of the program.

function head<T>(items: T[]) {
if (items) { // This check is unnecessary
return items[0].toUpperCase();
}
}
function foo(arg: 'bar' | 'baz') {
if (arg) { // This check is unnecessary
}
}
function bar(arg: string) {
return arg?.length; // ?. is unnecessary
}

Contrary to the source rule, this rule doesn’t trigger bindings that are assigned to multiple values. In the following example, the variable greeting is assigned to multiple values; hence it can’t be inferred to a truthy or falsy value.

let greeting = false;
function changeGreeting() {
greeting = "Hello World!"
}
if (greeting) {} // rule not triggered here
function head<T>(items: T[] | null) {
if (items) { // This check is necessary
return items[0].toUpperCase();
}
}
function foo(arg: 'bar' | 'baz' | null) {
if (arg) { // This check is necessary
}
}
function bar(arg: string | undefined) {
return arg?.length; // ?. is necessary
}