(Here is an example written using Typescript, but it applies to other cases as well)
class IMyInterface {
doC:(any) => any;
}
class Common {
commonProperty:any;
doA() {
}
doB() {
}
}
class ClassA extends Common {}
class ClassB extends Common implements IMyInterface {
doC(test:any) {
return true;
}
}
class Factory {
myClass: Common;
doSomething() {
// Property 'doC' does not exist on type 'Common'
this.myClass.doC('test');
}
}
Classes A and B are extensions of the Common class. Therefore, in the Factory class, myClass can be defined as a type Common.
However, Class B needs to implement IMyInterface, which is not included in the Common class. This results in an error being thrown by the Factory class, indicating that the interface method does not exist on the Common class.
What would be the best way to address this?
[Edited]
First and foremost, @basarat, thank you very much for your input. I still have some lingering questions,
What if there are additional classes that also implement IMyInterface?
class ClassC extends Common implements IMyInterface {
doC(test:any) {
return true;
}
}
class ClassD extends Common implements IMyInterface {
doC(test:any) {
return true;
}
}
class ClassE extends Common implements IMyInterface {
doC(test:any) {
return true;
}
}
In such a scenario, one might consider defining the doC() method in the Common class. However, it is also important to ensure that Class B, C, D, and E all must implement the doC() method.
Please offer some guidance on this matter.