TypeScript Version: 2.1.0-dev.20160918
Code
exportfunctionreturnAnyString(): string{return"";}exportfunctionacceptsOnlyFoo(value: "foo"): void{// ...}constvalue=returnAnyString();if(value==="foo")acceptsOnlyFoo(value);// error TS2345: Argument of type 'string' is not assignable to parameter of type '"foo"'.Expected behavior:
The === to narrow type string to a string literal type when checking for equality with a string literal. I would expect the above example to be the same as the following example which does work...
exportfunctionreturnAnyString(): string{return"";}exportfunctionacceptsOnlyFoo(value: "foo"): void{// ...}functionisFoo(s: string): s is "foo"{returns==="foo";}constvalue=returnAnyString();if(isFoo(value))acceptsOnlyFoo(value);Actual behavior:
The type is still string.
TypeScript Version: 2.1.0-dev.20160918
Code
Expected behavior:
The
===to narrow typestringto a string literal type when checking for equality with a string literal. I would expect the above example to be the same as the following example which does work...Actual behavior:
The type is still
string.