Avoid unnecessary if-else chains that only returns a boolean

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: typescript-best-practices/no-if-else-return

Language: TypeScript

Severity: Warning

Category: Best Practices

Description

This rule is designed to simplify your code by avoiding unnecessary if-else chains that only return a boolean. In JavaScript, it’s not necessary to use an if-else statement to return a boolean value from a function. Instead, you can return the result of the boolean expression. This makes your code shorter, cleaner, and easier to understand.

Non-Compliant Code Examples

function getFoo() {
  const foo = computeFoo();
  if (foo) {
    return true;
  } else {
    return false;
  }
}

Compliant Code Examples

function getFoo() {
  const foo = computeFoo();
  return foo;
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis