I'm facing an issue with the following code snippet
interface BaseA {
a: number;
}
interface SpecialA extends BaseA {
b: number;
}
type A = BaseA | SpecialA
const a = {
a: 5, b: 5
} as A
console.log(a.b)
Even though I thought the code was correct, I encountered the error message
Property 'b' does not exist on type 'A'.
Property 'b' does not exist on type 'BaseA'
It appears that the actual definition of type A is not what I intended it to be. I was expecting it to match the definition below:
interface A {
a: number;
b?: number;
}
This leaves me with the following questions:
- What caused the discrepancy between the defined type A and the expected type A?
- Is there a way to define the expected type A without manually defining it?
Note: I am required to use the type SpecialA unchanged in certain parts of my code, so directly defining the expected A type is not feasible.