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

Unexpected TS2365: Build:Operator '==' cannot be applied to types 'true' and 'false' #12772

Closed
sgreer opened this issue Dec 8, 2016 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@sgreer
Copy link

sgreer commented Dec 8, 2016

After updating to TypeScript 2.1, I am receiving an error when compiling a project that compiled under TS 2.0.

Below is the snippet of code that produces the error.

var id1 = true, id2 = true;
$(selector, $(question)).each((i, iteminput) => {
    $id = $(iteminput);
    id = $id.attr("id");
    if (!$id.is(":checked") && $id.is(":visible") && $id.data("val-required") == "Required") {
        if (i == 0)
            id1 = false;
        else if (i == 1)
            id2 = false;
    }
});
if (id1 == false && id2 == false) {
    valid = false;
    this.showValidationPrompt({
        message: "* This question is required.",
        elementid: id,
        placement: 0,
        promptType: 1,
        containerSelector: ".cq-wrapper .cq-input"
    });
}

Expected behavior: That this snippet will compile without errors.

Actual behavior: I receive the following error message:

error TS2365: Build:Operator '==' cannot be applied to types 'true' and 'false'.

due to this condition evaluation:

if (id1 == false && id2 == false) {

@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2016

Following the control flow id1 does not seem to be assigned any value other than true. the function expression is not considered part of the flow, since it is not guaranteed to execute. the result is at the conditionid1 has the type true and the check is considered invalid.

A workaround would be to either cast to boolean as var id1 = true as boolean which widens the type of the variable form just true to true | false. or change the check to be for truthiness if (id1 && id2).

Please see #12729 for more details.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Dec 8, 2016
@sgreer
Copy link
Author

sgreer commented Dec 8, 2016

Okay, the fact that the function expression is not guaranteed to run and the result would be "true == false" makes sense to me. I will use the cast to boolean as a resolution.

Thanks for the prompt reply. 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants