Imagine a scenario where I am dealing with two specific interfaces:
interface Box {
x: number
y: number
}
and
interface ColouredBox {
x: number
y: number
colour: string
}
For the sake of this discussion, let's assume that the interfaces cannot be modified.
When it comes to creating objects, the code I typically use looks something like this:
let a: Box|ColouredBox = {x: 1, y: 2}
if( something ){
a.colour = "Blue" // Compilation Error
}
Attempting to assign a value to a.colour = "Blue"
results in the following error:
Error:(24, 26) TS2339: Property 'colour' does not exist on type 'Box'.
What is causing this error? Is this a limitation of the TypeScript compiler? Are there any alternative approaches I could take without completely rebuilding the object?