-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
TypeScript 3.7 already added uncalled function checks. Is that what you're asking for? |
@DanielRosenwasser not necessarily uncalled
|
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 |
@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 |
I suggest that the bang operator should also be strict (only work on bools). So the alternative writing would be |
I'm very surprised TS doesn't already have this. While I get that allowing truthy/falsy in an 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 Given that this is a type-checking issue, and currently it's possible to inadvertently pass a non-boolean expression into an i.e. a |
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, etcbasically 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 beenif (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:
The text was updated successfully, but these errors were encountered: