Closed
Description
TypeScript Version: 2.1.0-dev.20160918
Code
export function returnAnyString(): string {
return "";
}
export function acceptsOnlyFoo(value: "foo"): void {
// ...
}
const value = 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...
export function returnAnyString(): string {
return "";
}
export function acceptsOnlyFoo(value: "foo"): void {
// ...
}
function isFoo(s: string): s is "foo" {
return s === "foo";
}
const value = returnAnyString();
if(isFoo(value))
acceptsOnlyFoo(value);
Actual behavior:
The type is still string
.