Closed
Description
Hi,
TS Version: 1.4
At present there doesn't appear to be a way to define an enum in a union type and have it narrowed correctly. Here's an example:
function f1(x: number | string){
if (typeof x === "number") {
return x + 10;
}
else {
return x.length;
}
}
enum Colour { red, blue }
function f2(x: Colour | string) {
if (typeof x === "number") {
return x + 10; // Error because x is Colour | string
}
else {
return x.length;
}
}
var x = f1(1);
var y = f2(Colour.blue);
The type guard typeof x === 'number'
should probably narrow the type correctly and not report an error in f2
.