Reputation: 493
I have an interface and a class:
interface Widget {
widgetStuff(): void;
}
class Foo implements Widget {
widgetStuff(): void {
}
}
I want to track all of the Widget
s in another class, and an index signature seems to be the easiest way:
class WidgetCatalog {
private readonly mapping: { [key: string]: Widget };
constructor() {
this.mapping = {
Foo // <= Error here
}
}
}
I'm getting this error: error TS2322: Type 'typeof Foo' is not assignable to type 'Widget'.
Property 'widgetStuff' is missing in type 'typeof Foo'.
I'm not sure what Im missing here, since it seems to be a pretty straightforward implementation. Do I need to specify the value type in the index signature differently?
Upvotes: 0
Views: 118
Reputation: 493
@jcalz pointed me in the right direction. This version works:
interface Widget {
widgetStuff(): void;
}
class Foo implements Widget {
widgetStuff(): void {
}
}
class WidgetCatalog {
// Change to mapping of Widget constructors
private readonly mapping: { [key: string]: new(...args) => Widget };
constructor() {
this.mapping = {
Foo
}
}
}
Upvotes: 1