Closed
Description
TypeScript Version:
1.8.x and nightly
Code
class C1 { item: string }
class C2 { item: string[] }
class C3 { item: string }
function Foo(x: C1 | C2 | C3): string {
if (x instanceof C1)
return x.item;
else if (x instanceof C2)
return x.item[0];
else if (x instanceof C3)
return x.item; // Error property item does not exist on C2
}
Expected behavior:
Code should compile
Actual behavior:
Code has an error as shown
More
The following works i.e. if C1
and C3
differ in structural compatibility:
class C1 { item: string }
class C2 { item: string[] }
class C3 { items: string }
function Foo(x: C1 | C2 | C3): string {
if (x instanceof C1)
return x.item;
else if (x instanceof C2)
return x.item[0];
else if (x instanceof C3)
return x.items; // Ok
}
🌹