Skip to content

strict boolean expressions compilerOption #38826

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

Open
4 tasks done
venil7 opened this issue May 28, 2020 · 8 comments
Open
4 tasks done

strict boolean expressions compilerOption #38826

venil7 opened this issue May 28, 2020 · 8 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@venil7
Copy link

venil7 commented May 28, 2020

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.
@DanielRosenwasser
Copy link
Member

TypeScript 3.7 already added uncalled function checks. Is that what you're asking for?

@venil7
Copy link
Author

venil7 commented May 29, 2020

@DanielRosenwasser not necessarily uncalled
I refer to

if (condition) ..
while (confition)...
(condition) ? .. : ..  

condition to be checked to be strictly boolean, not just truthy / falsy

@jgbpercy
Copy link

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.

@chharvey
Copy link

chharvey commented Jun 4, 2020

@DanielRosenwasser With respect to uncalled function checks, you’re right about support for conditional statements:

if (uncalledFunction) { return 1; } else { return 0; }
//  ^ ts2774 "This condition will always return true …"

but I don’t see the same thing with the ternary expression:

return (uncalledFunction) ? 1 : 0;
//      ^ no error

or with multiple conditions:

if (uncalledFunction1 || uncalledFunction2) { return 1; } else { return 0; }
//  ^ no error           ^ no error

@NN---
Copy link

NN--- commented Jun 4, 2020

@DanielRosenwasser
Copy link
Member

@chharvey it should work with ternaries in 3.9

@NN--- I think so

#7746 seems to be the canonical-ish issue for this. We discussed pedantic-mode flags in #38905.

@Rudxain
Copy link

Rudxain commented 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

@rbdmorgan
Copy link

I'm very surprised TS doesn't already have this. While I get that allowing truthy/falsy in an if is in keeping with the spirit of JS, it amounts to a significant, often detrimental relaxation of type-checking that really isn't in the spirit of TS or what it's for, IMO.

We're currently in the process of migrating from eslint and typescript-eslint to biome (as it's a LOT faster) and this is the one rule we need that biome doesn't have and on which we've come to rely. As I understand it, the reason for this is, being a pure linter, biome (unlike typescript-eslint) doesn't invoke tsc (nor should it need to, really), which is part of the reason it's faster.

Given that this is a type-checking issue, and currently it's possible to inadvertently pass a non-boolean expression into an if and other conditionals, TS really should at least have the option to prevent this. It shouldn't defer this to a third-party linter.

i.e. a --strictBooleanExpressions option would be ideal, I think for a lot of developers, in this increasingly TS-first world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

8 participants