Tom
Tom

Reputation: 5121

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.

Upvotes: 4

Views: 6969

Answers (2)

Tom
Tom

Reputation: 5121

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.

Upvotes: 8

Dean Chalk
Dean Chalk

Reputation: 20451

I may be wrong, but isnt component: typeof BaseListComponent declaring component to be of type Type

shouldn't it just be component: BaseListComponent

Upvotes: 0

Related Questions