question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

strict boolean expressions compilerOption

See original GitHub issue

Search Terms

require strict boolean

Suggestion

add yet another --strict check into compilerOptions, to only allow expressions that evaluate to Boolean for all conditional branching - if, for, while, ?: ternary operator, etc

basically this TSLint rule, built into TS compiler https://palantir.github.io/tslint/rules/strict-boolean-expressions/

Use Cases

often people check the existence of function, before calling it: if (somFunc) someFunc()

or they think that function is actualy a property, and hence dont call it

if (someVal)... . where it should have been if (someVal()) ...

if if required a strict boolean expression, both of the above would have been errors.

Examples

instead above examples would be written: if (!!someFunc) someFunc()

and for someValue():boolean

if (someValue())..

Checklist

My suggestion meets these guidelines:

  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jgbpercycommented, May 29, 2020

As a strict consumer, I don’t think I would want this functionality, and so I wouldn’t want it to be under the main --strict flag.

0reactions
Rudxaincommented, Oct 3, 2022

I suggest that the bang operator should also be strict (only work on bools). So the alternative writing would be Boolean(any) instead of !!any, this makes the coercion more explicit

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript-eslint/strict-boolean-expressions.md at main - GitHub
Forbids usage of non-boolean types in expressions where a boolean is expected. boolean and never types are always allowed. Additional types which are ......
Read more >
strict-boolean-expressions - Rule
Rule: strict-boolean-expressions. Restricts the types allowed in boolean expressions. By default only booleans are allowed. The following nodes are checked:
Read more >
typescript-eslint/strict-boolean-expressions lint rule
The allow-boolean-or-undefined doesn't exist for this rule on ESLint, the option equivalent is the allowNullable. The correct syntax is:
Read more >
JS-0382 · Restrict the types allowed in boolean expressions
Restrict the types allowed in boolean expressions JS-0382 ... This rule requires the strictNullChecks compiler option to be turned on to function correctly....
Read more >
Restricts the types allowed in boolean expressions ( strict ...
Requires that any boolean expression is limited to true booleans rather than casting another primitive to a boolean at runtime. It is useful...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found