Shouldn't this fail?
class Animal { }
class Person { }
type MyUnion = Number | Person;
var list: Array<MyUnion> = [ "aaa", 2, new Animal() ]; // Shouldn't this fail?
var x: MyUnion = "jjj"; // Shouldn't this fail?
Is there a way to enforce type checking in this case?
TypeScript handles type compatibility based on
structural subtyping
.In particular for classes:
More info at https://www.typescriptlang.org/docs/handbook/type-compatibility.html#classes