My goal is to enforce the requirement that any class implementing the IGeneratable
interface must also provide a IGeneratorConstructor<T>
, where T
represents the class implementing IGeneratable
.
Is this achievable in TypeScript (specifically version 3.8.3)? If so, how can it be done?
export class Example implements IGeneratable {
public generator = ExampleGenerator;
}
export class ExampleGenerator implements IGenerator<Example> {
private _component: Example;
constructor(context: GeneratorContext<Example>) {
this._component = context.component;
}
getId(): string {
return 'myID';
}
build(): void {
// Perform actions using information from this.component
}
}
The following are the current types being used:
export interface IGeneratable {
generator: IGeneratorConstructor<any>; // Assistance needed here
}
export interface GeneratorContext<TComponent extends IGeneratable> {
scene: Scene;
component: TComponent;
}
export interface IGenerator<TComponent extends IGeneratable> {
getId(): string;
build(): void;
}
export interface IGeneratorConstructor<TComponent extends IGeneratable> {
new (context: GeneratorContext<TComponent>): IGenerator<TComponent>;
}