I would like to make a factory method for all my Axios Api classes (generated by OpenAPI generator).
So Open-API generates me a BaseAPI class with a constructor and dozen of Api classes extending this BaseAPI corresponding to my Backend Controllers.
So i tried that function:
import { BaseAPI } from "@/api/base";
abstract class AbstractControllerHandler {
protected createApi<T extends typeof BaseAPI>(clazz: T) {
return new clazz(
undefined,
"",
AxiosInstanceHolder.getInstance().getAxiosInstance(),
);
}
Which is used by this other class:
import { CrudCapitalsourceControllerApi } from "@/api";
class CapitalsourceControllerHandler extends AbstractControllerHandler {
private api: CrudCapitalsourceControllerApi;
public constructor() {
super();
this.api = super.createApi(CrudCapitalsourceControllerApi);
}
}
CrudCapitalsourceControllerApi
is defined as class CrudCapitalsourceControllerApi extends BaseAPI
and is generated by OpenAPI.
I’m getting a ts(2739) basically stating that createApi
returns type BaseAPI
which cannot be assigned to a variable of type CrudCapitalsourceControllerApi
.
I guess I have to specify a return type for createApi to get it to work, but T
as return type does not work. It raises a ts(2322) and I don’t return a type I return an instance… so any idea of how I can fix this? I guess I would need something as “instanceof T” for the return type declaration