I have an interface and a class:
interface Widget {
widgetStuff(): void;
}
class Foo implements Widget {
widgetStuff(): void {
}
}
I want to track all of the Widgets 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?
this.mapping.Foowill beFoo, the class constructor that makes newWidgets. It's not aWidgetitself. So eithermappingshould holdWidgetconstructors, in which case it's mis-annotated, orthis.mappingshould be initialized to{Foo: new Foo()}or something.