Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define typeof abstract class in typescript

How do I make typeof BaseListComponent below assignable?

The code below throws the following error:

Type '({ title: string; component: typeof QuestionsListComponent; } | { title: string; component: ...' is not assignable to type '{ title: string; component: typeof BaseListComponent; }[]'.

Excerpt:

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}
]

P.S this is a simplified excerpt from an angular application and so there is logic behind this madness.

like image 243
Tom Avatar asked Mar 09 '23 20:03

Tom


1 Answers

Using angular's Type interface was the solution. A better explanation of what Type is can be found here. Steps to solve:

  1. Import Type like so: import { Type } from "@angular/core";

  2. Replace typeof BaseListComponent with Type<BaseListComponent>


For people coming here who are not using angular, this thread might be helpful.

like image 189
Tom Avatar answered Mar 21 '23 08:03

Tom