Skip to content
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

Proposal: option to disable implicit boolean cast in conditions #19191

Closed
impworks opened this issue Oct 15, 2017 · 4 comments
Closed

Proposal: option to disable implicit boolean cast in conditions #19191

impworks opened this issue Oct 15, 2017 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@impworks
Copy link

I came across a very silly bug in my code which the Typescript compiler did not catch. Consider the following snippet of code:

class MyClass {
    public /*get*/ flag(): boolean {
        return false;
    }

    public doStuff() {
        if(this.flag) {
            alert('hello');
        }
    }
}

The alert will always be executed, because the property has been mis-declared as a method. Instead of invoking the getter and checking its return value, we instead evaluate the body as truthy.

While this behaviour is expected in many cases, it would be nice to have a compilation flag to enable additional checks, for example - noImplicitBool. It would require expressions in classic control structures (if, while, for) to be explicitly of the boolean type.

The flag should not affect the behaviour of the || operator, which is often used for coalescing values of arbitrary types.

@aluanhaddad
Copy link
Contributor

See #7746

@ajafff
Copy link
Contributor

ajafff commented Oct 15, 2017

TSLint has a rule strict-boolean-expressions that does exactly what you want: https://palantir.github.io/tslint/rules/strict-boolean-expressions/

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 16, 2017
@RyanCavanaugh
Copy link
Member

See #9041

@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Oct 30, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants