我正在尝试使用Typescript为React Native中的状态组件做一个临时组件,我想将其用作装饰器。我的类型定义如下:
export function hoc<T extends ComponentClass>(target: any): T {
return (target: any) =>
class MyClass extends Component<any, any> {
}
}
但是我收到错误Type '(target: any) => typeof MyClass' is not assignable to type 'T'
我应该改变些什么以使其与打字稿正确?
答案 0 :(得分:0)
您可以使用这种类型来表示每个实例都返回与其被调用的相同类型的实例:
class MyClass {
public each<T extends MyClass>(callback: (item: T) => void): this {
/* Loop through an array and apply the callback */
return this;
}
}
class Derived extends MyClass {
foo: string;
}
let derived = new Derived;
derived = derived.each(() => {});
这可能对您有帮助