Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if statements only allow boolean and nullish expressions (not falsey ones) #36

Open
coolaj86 opened this issue Aug 31, 2022 · 0 comments
Labels
syntax the strict subset of JavaScript that is AJScript

Comments

@coolaj86
Copy link
Collaborator

This rule is taken from Go.

❌ Bad Example

if (!arr.length) {
    throw new Error("no items in array");
}

✅ Good Example

if (0 === arr.length) {
    throw new Error("no items in array");
}

Rationale

It's more difficult to do proper checking of type-constrained conditions if 0 and "" are considered valid boolean expressions.

For example:

/** @type {Number|String} foo */

if (!foo) {
  // if we constrain the boolean expression then we can detect
  // that there's a conflict between the code and the type:
  // `foo` is not optional according to the type, but perhaps the user thinks it can be
}
@coolaj86 coolaj86 added the syntax the strict subset of JavaScript that is AJScript label Aug 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
syntax the strict subset of JavaScript that is AJScript
Projects
None yet
Development

No branches or pull requests

1 participant