接口的打字稿声明问题
Typescript declaration issue with interface
我正在尝试声明一个接口以允许 FormatComponent 'dictionary' 允许添加一堆不同的组件 - 所有组件都实现了该接口。这是一个类型查找,而不是一个实例,所以我想我明白为什么我不能在下面的格式组件中使用接口——只有 'any' 有效。存在编译器错误,指示类型未实现数据 - 使用接口时。
但是,类型安全并在 FormatComponents 声明中使用接口会更好 - 知道如何实现它吗?
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
};
使用类型 {new(): FormatComponent}
匹配实现 FormatComponent
.
的 class(或者更确切地说,构造函数)
我正在尝试声明一个接口以允许 FormatComponent 'dictionary' 允许添加一堆不同的组件 - 所有组件都实现了该接口。这是一个类型查找,而不是一个实例,所以我想我明白为什么我不能在下面的格式组件中使用接口——只有 'any' 有效。存在编译器错误,指示类型未实现数据 - 使用接口时。
但是,类型安全并在 FormatComponents 声明中使用接口会更好 - 知道如何实现它吗?
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
};
使用类型 {new(): FormatComponent}
匹配实现 FormatComponent
.