I have a class MyClass that uses another class MainProvider. The thing is that the normal MainProviderclass doesn't work like I want, so I created another class MyProvider which has the same functions (implement the same interface as Provider).
My problem is that I can't use MyProvider in my class (MyClass), because even if the class MyProvider implements the same interface, there is a mismatch with a property (here getFetchUrl).
It is a private property in the MainProvider class, so it's not present in the interface. Still, I can't use MyProvider in my class. I get this error:
Argument of type 'MyProvider' is not assignable to parameter of type 'MainProvider'. Property 'getFetchUrl' is missing in type 'MyProvider' but required in type 'MainProvider'.
I tried to add it in my own class, but get this error instead Argument of type 'MyProvider' is not assignable to parameter of type 'MainProvider'. Types have separate declarations of a private property 'getFetchUrl'.
How can I use one as the substitute for the other ?
Example:
declare abstract class ProviderInterface {
abstract baseUrl: string;
abstract foo(): void;
}
class MainProvider implements ProviderInterface {
baseUrl: string;
private getFetchUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
foo() {
return "hello";
}
}
class MyProvider implements ProviderInterface {
private _baseUrl: string;
private getFetchUrl: string;
constructor(baseUrl: string) {
this._baseUrl = baseUrl;
}
foo() {
return "hey";
}
get baseUrl() {
return this._baseUrl;
}
}
class MyClassUsingProvider {
private _provider: MainProvider;
constructor(provider: MainProvider) {
this._provider = provider;
}
}
const myProvider = new MyProvider("http://127.0.0.1:3000");
const myClass = new MyClassUsingProvider(myProvider);