This is my Typescript interface/class structure that I have:
interface IBaseOptions {
homeUrl: string;
}
abstract class BaseApp<TOptions extends IBaseOptions = IBaseOptions> {
constructor(
private options: TOptions
) {
// does nothing
}
}
// -----------------------------
interface ICalls {
getValue: () => number;
}
interface IOptions<TCalls extends ICalls = ICalls> extends IBaseOptions {
calls: TCalls;
}
class App<TOptions extends IOptions<TCalls> = IOptions<TCalls>, TCalls extends ICalls = ICalls> extends BaseApp<TOptions> {
-------- (ts2744 error here)
constructor(options: TOptions) {
super(options);
}
}
class SubApp extends App {
// whatever implementation
}
I would like to provide the defaults so that I don't have provide concrete types for my options and calls. The way I defined my types results in a compile error (ts2744 error).
I would also like to avoid swapping my generic types (with constraints and defaults) so that I would keep the first generic type to be options and calls second.
Is there any way to first define generic types with contraints and afterwards set their defaults?
You can check this Playground Link