我如何声明typeof B
和任何扩展B
的类的类型?我已经创建了一个类型,该类型可以接受扩展Component
类的类数组,但这在TSC 3(1.4 | 2.1)上不起作用。
export class Component<S> {}
export type ComponentClass = new () => Component<{ message: string }>;
export interface ComponentFlags<S> {
imports?: (typeof Component | new () => Component<any>)[]
}
export class Message extends Component<State> {}
export class Dialog extends Component<State> {
constructor(el: HTMLDivElement) {
super({
imports: [Message]
});
}
}
然后在编译时会引发错误。
type 'typeof Message' is not assignable to type 'typeof Component'.
答案 0 :(得分:2)
使用此:
const a = <T extends new(...args: any) => B> (p: T|B) => p;
针对您的情况:
export type NewComponnent = new (...args: any[]) => Component<any>;
export interface ComponentFlags<S> {
imports?: (components: NewComponent[]) => void;
// OR
// imports?: (...components: NewComponent[]) => void;
}