strict boolean expressions compilerOption
See original GitHub issueSearch 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:
- Created 3 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
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.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