打字稿:无法分配给类型参数

时间:2020-09-14 22:23:31

标签: typescript

我有这个抽象类:

export abstract class BaseController {
  private path: string;
  private router: Router;
  constructor() {}
  private initializeRoutes() {}
  public getPath = (): string => {
    return this.path;
  };
  public getRouter = (): Router => {
    return this.router;
  };
}

和班级:

export class Controller {
  private controllers: BaseController[];
  constructor(controllers: BaseController[]) {
    this.controllers = controllers;
  }
  public registerControllersInContainer = () => {
    for (let controller of this.controllers) {
      container.registerSingleton<BaseController>('Controllers', controller);
    }
  };
}

用法:

类测试扩展了BaseController {}

const controllerManager = new Controller([new test()])

但是当我在容器中注册我的课程时使用打字稿时,我得到了这个错误:

Argument of type 'BaseController' is not assignable to parameter of type 'InjectionToken<BaseController>'.
  Type 'BaseController' is missing the following properties from type 'DelayedConstructor<BaseController>': wrap, reflectMethods, createProxy, createHandlerts(2345)

我认为这就是为什么我需要确保我的控制器不是类:

  public registerControllersInContainer = () => {
    for (let controller of this.controllers) {
      container.registerSingleton<BaseController>('Controllers', controller);
    }
  };

我不明白如何解决这个问题。

0 个答案:

没有答案