reactjs - 状态组件的 React Native 高阶组件
问题描述
我正在尝试使用 Typescript 为 React Native 中的状态组件做一个 hoc,我想将它用作装饰器。我的类型定义如下:
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'
我应该改变什么以使其正确使用打字稿?
解决方案
您可以使用此类型来表示每个返回的实例都与调用它的类型相同:
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(() => {});
这可能会帮助你
推荐阅读
- swift - 领域项目添加到表格视图时出现两次
- jquery - JQuery DataTableset.net 第一个和最后一个事件?
- shake-build-system - 抖动构建:如何处理稍后发现所需文件的情况
- wpf - 从枚举绑定的 wpf 组合框中查找所选项目
- python - 从 Mininet Python API 启动的脚本在一段时间后停止工作
- reactjs - 反应中的静态函数内的this.state
- sql - 在一个查询中创建所有数据库“事物”
- javascript - AppSync HTTP 解析程序 IAM 授权错误
- node.js - 如何从数据库中检索数据并使用 exrpessjs 相应地呈现它
- php - Soap 请求在 SoapUI 中有效,但在 Wordpress 插件中无效