在typescript中定义typeof抽象类

Tom*_*Tom 4 abstract-class types class typescript angular

我如何在typeof BaseListComponent下面指定?

下面的代码引发以下错误:

类型' ({ title: string; component: typeof QuestionsListComponent; } | { title: string; component: ...'不能分配给类型'{ title: string; component: typeof BaseListComponent; }[]'.

摘抄:

abstract class BaseListComponent {
  protected title
}

class WeatherListComponent extends BaseListComponent {}

class QuestionsListComponent extends BaseListComponent {}

let containerItems: Array<{title: string, component: typeof BaseListComponent}>;
containerItems = [
  {title:'TypeScript', component: QuestionsListComponent},
  {title:'Angular2', component: QuestionsListComponent},
  {title:'Weather', component: WeatherListComponent}
]
Run Code Online (Sandbox Code Playgroud)

PS这是一个角度应用程序的简化摘录,所以这种疯狂背后有逻辑.

Tom*_*Tom 6

使用angular的Type界面是解决方案.Type可以在这里找到更好的解释.要解决的步骤:

  1. Type像这样导入:import { Type } from "@angular/core";

  2. 替换typeof BaseListComponentType<BaseListComponent>


对于那些不使用角度的人来说,这个帖子可能会有所帮助.