I have a scenario where an abstract base class has an abstract method. A second class extends this base class and adds overloaded methods for this abstract method, without actually implementing the abstract method. And a third class which extends the second class, which actually implements the abstract base class method (playground):
abstract class A {
public abstract test(o?: object): unknown;
}
abstract class B extends A {
public test(o?: object): unknown;
public test(s: string): unknown;
public test(n: number): unknown;
public test(sOrN?: string | number | object): unknown {
if (typeof sOrN === "string") {
return String(sOrN);
}
return Number(sOrN);
}
}
class C extends B {
public test(o?: object): unknown {
return undefined;
}
}
A similar question was asked here, but that doesn't discuss an abstract base method. The suggestion there is to include the abstract method as overloading signature in class B. However, I cannot implement it there. It will be implemented in class C.
With that approach I get an error in class C:
Property 'test' in type 'C' is not assignable to the same property in base type 'B'. Type '(o?: object | undefined) => unknown' is not assignable to type '{ (o?: object | undefined): unknown; (s: string): unknown; (n: number): unknown; }'. Types of parameters 'o' and 's' are incompatible. Type 'string' is not assignable to type 'object'.(2416)
How can this be solved without having to include the abstract base method signature in the overload?