I've an abstract clas like
class AbsClass {
type: 'A'|'B'
constructor(){}
methodA():void
}
then i have 2 classes thet extends AbsClass:
class AClass extends AbsClass {
constructor(){
super()
this.type = 'A'
}
methodA(){
console.log('my type is A')
}
a(){
console.log('a method')
}
}
class BClass extends AbsClass {
constructor(){
super()
this.type = 'B'
}
b(){
}
methodA(){
console.log('my type is B')
}
}
If i have an array of instances that can be of AClass or BClass, in the following code i want that typescript infer the instance type by the property type...
const instances:AbsClass[] = [
new AClass(),
new BClass()
]
for(const inst of instances){
if(inst.type == 'A') { //here i want typescript infer the type because only AClass can have 'A' type
inst.a() //AbsClass has not "a" method
}
}