You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//This one, tsc give me an error.constlab='icon-yes';if(lab==='321'){}//<- [ts] Operator '===' cannot be applied to types '"icon-yes"' and '"321"'.//All of below are correct.constlb: string='icon-yes';if(lb==='321'){}letla='icon-yes';//<- this one , I just replace `const` to `let`. Why it became correct?if(la==='321'){}constlabc='icon-yes';if(labc==='icon-yes'){}//<- compare the error case. Do not understand.letlabcd='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.
The text was updated successfully, but these errors were encountered:
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:
constlabc='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.
leta: 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
leta: "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"'.// ...}}
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.7.0-dev.201xxxxx
Code
Expected behavior:
const lab = 'icon-yes';
I think the variablelab
should bestring
type by type inference.All of these cases should be correct.
Actual behavior:
Can not figure out why.
The text was updated successfully, but these errors were encountered: