Following the guidelines outlined in section 3.4 of the specification, there is an issue with compilation when trying to assign a boolean value to a variable declared as a string or number:
let s: string | number;
s = false; // Error - not a string or a number
Given classes A, B, and C, it would be logically consistent to expect similar compilation errors when assigning different types to a variable declared as A or B. However, surprisingly, this code compiles without any issues:
let a: A | B;
a = new C();
a = "hi";
a = {}; // No error - a can be assigned anything!
Is there a workaround to enforce stricter type checking in this scenario? Furthermore, is this behavior intentional or a bug in TypeScript's design? If intended, the reasoning behind this design decision seems unclear.