Skip to content

strict boolean expressions compilerOption #38826

Open
@venil7

Description

@venil7

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions