我想创建一个基类,将其包装在一个容器中,然后在我的其他组件中进行扩展。反应打字稿扩展容器
下面是一个简单的例子:
let state = new State();
class Base extends React.Component<{}, {}> {
}
const Container = state.connect(
() => {
return {
}
},
() => {
return {
}
}
)(Base);
class S extends Container {
}
然而,这是返回一个错误:
`error TS2507: Type 'any' is not a constructor function type.`.
如果我直接延长Base
,它工作正常,但后来我怎么会得到访问状态并发送我放入容器的操作?
UPDATE
我创建了以下接口(略去模板为简单起见)
interface IState {
connect: (
mapStateToProps:() => any,
mapDispatchToProps:() => any,
) => (component: typeof React.Component) => typeof React.Component
}
let state: IState = new State();
现在类扩展不会引发任何错误。但是,原来的Base
类永远不会被调用!只调用Connect
方法的构造函数。
这里的想法是,我将有一个抽象的组件和容器(所有的调度动作都在容器上)。然后我可以从其他类中扩展这个组件。但是,由于容器包含所有调度操作,因此我需要扩展容器,而不是组件。
也许你应该申报HOC特殊类型:看这里:http://stackoverflow.com/questions/41499831/type-annotation-for-react-higher-order-component-using-typescript –
超过在同一天的3个问题:) –