My attempt at creating an abstract class is not going as smoothly as I hoped. I suspect my limited knowledge of TypeScript is the primary issue, even though this seems like a common scenario.
The abstract class I'm working on is called Program. It contains various functions and attributes.
abstract class Program {
someString: string = "bob";
someFunc(): void {
return;
}
someOtherfunc: void {
this.childFunc();
return;
}
}
Additionally, I have an interface called 'IProgram' which looks like this:
interface IProgram {
childFunc: () => void;
}
I created a class called Child that extends the Program class and implements the 'IProgram' interface like this:
class Child extends Program implements IProgram {
childFunc(): void {
console.log("Hello World")
}
}
Despite my efforts, I'm struggling to make it work without encountering some undesirable behavior.
I attempted to add an index signature to 'Program'. While this eliminated TS errors, it also allowed any operation in the child program, which I want to avoid.
Another approach I tried was declaring 'childFunc' as type any in the Abstract class, but then it complains that it should be a function, not a member.
I even created a minimal StackBlitz to showcase the issue:
https://stackblitz.com/edit/typescript-po7hbo
My ultimate goal is for the abstract class to ensure that all its children have the specified functions and properties.