Skip to content

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

Closed
@mrdulin

Description

@mrdulin

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions