Description
TypeScript Version: 2.3 +
Code
type MyType = 'meep'|'bidi';
type ThatType = 'moop'|'meep';
interface Foo {
isfoo: 'true';
foo: MyType;
}
interface Bar {
isfoo: 'false';
foo: string;
}
let arr: Array<Foo|Bar>;
arr = [{foo: 'meep', isfoo: 'true'},
{foo: 'moop', isfoo: 'false'}];
Expected behavior: This should compile. The discriminant isfoo
should be enough to figure out that the first element of the array can only be a Foo and the second is a Bar.
Actual behavior:
ERROR(15,1): : Type '({ foo: string; isfoo: "true"; } | { foo: string; isfoo: "false"; })[]' is not assignable to type '(Foo | Bar)[]'.
Type '{ foo: string; isfoo: "true"; } | { foo: string; isfoo: "false"; }' is not assignable to type 'Foo | Bar'.
Type '{ foo: string; isfoo: "true"; }' is not assignable to type 'Foo | Bar'.
Type '{ foo: string; isfoo: "true"; }' is not assignable to type 'Foo'.
Types of property 'foo' are incompatible.
Type 'string' is not assignable to type 'MyType'.
However, if Foo's foo
is declared as a string
, then it compiles fine. If Bar's foo
is declared as a ThatType
, then it also compiles fine.