2

I'm trying to declare an interface to allow a FormatComponent 'dictionary' to allow for a bunch of different components to be added - all implementing the interface. This is a type lookup, not an instance, so I think I understand why I can't use the interface in Format Components below - only 'any' works. There is a compiler error indicating the type doesn't implement data - when the interface is used.

But, it would be nicer to be type safe and use the interface in the declaration of FormatComponents - any ideas how to achieve it?

export interface FormatComponent {
    data: any;
}

export class ContactFormatComponent implements FormatComponent{
    data: any;
}

export const FormatComponents: { [id: string]: any } = {
    //I want FormatComponent instead of any
    'co-contacts': ContactFormatComponent,
    'other': OtherComponent // also implements the interface
};

1 Answer 1

3

Use the type {new(): FormatComponent} to match a class (or rather, constructor) that implements FormatComponent.

Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.