I'm faced with a code snippet that looks like this.
class Base{
private getData(): Data | undefined{
return undefined
}
public get output(): Data | undefined {
return {
data: this.getData()
}
}
}
class A extends Base{
private getData(){
return getDatasFromOutside()
}
//......
}
class B extends Base{
//......
}
let x = new A()
let x2 = new B()
x.output.data // I want this to be considered as type Data
x2.output.data // but in this case, it should remain undefined
In the current setup, typescript interprets x.output.data
as Data | undefined
when it is actually just Data
.
Is there a way to correct this issue?
Or would it be more appropriate to avoid having A and B share the same base class altogether?