Skip to content

[ts] Operator '===' cannot be applied to types '"aaa"' and '"bbb"'. #19831

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

Closed
mrdulin opened this issue Nov 8, 2017 · 4 comments
Closed

[ts] Operator '===' cannot be applied to types '"aaa"' and '"bbb"'. #19831

mrdulin opened this issue Nov 8, 2017 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mrdulin
Copy link

mrdulin commented Nov 8, 2017

TypeScript Version: 2.7.0-dev.201xxxxx

Code

//This one, tsc give me an error.
const lab = 'icon-yes';
if (lab === '321') {}  //<- [ts] Operator '===' cannot be applied to types '"icon-yes"' and '"321"'.

//All of below are correct.
const lb: string = 'icon-yes';
if (lb === '321') {}

let la = 'icon-yes';  //<- this one , I just replace `const` to `let`. Why it became correct?
if (la === '321') {}

const labc = 'icon-yes';
if (labc === 'icon-yes') {}  //<- compare the error case. Do not understand.

let labcd = 'icon-yes';
if (labcd === '321') {}

Expected behavior:

const lab = 'icon-yes'; I think the variable lab should be string type by type inference.

All of these cases should be correct.

Actual behavior:

Can not figure out why.

@ghost
Copy link

ghost commented Nov 8, 2017

In const lab = "icon-yes", lab can never have a value other than "icon-yes" because it is a const. So it is safe and correct to give it a string literal type.
If you explicitly write : string we will use that. If the variable is mutable it might not always have that value, so it gets the string type.

As for this case:

const labc = 'icon-yes';
if (labc === 'icon-yes') {}  //<- compare the error case. Do not understand.

It probably should be an error because the === operation can never return false, but currently we only have the tests for === that can never return true.

@ghost ghost added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 8, 2017
@fenying
Copy link

fenying commented Nov 12, 2017

@Andy-MS See also this

let a: boolean = false;

try {

    a = true;
    // do something else.
    a = false;
}
catch (e) {

    if (true === a) { // [ts] Operator '===' cannot be applied to types 'true' and 'false'.

        // ...
    }
}

and

let a: "a" | "b" = "a";

try {

    a = "b";
    // do something else.
    a = "a";
}
catch (e) {

    if ("b" === a) { // [ts] Operator '===' cannot be applied to types '"b"' and '"a"'.

        // ...
    }
}

@ghost
Copy link

ghost commented Nov 13, 2017

@fenying See #18916 and #18478.

@typescript-bot
Copy link
Collaborator

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

@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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants