使用Typescript的React高阶组件的类型注释
返回类型
typeof React.Component是真实的,但对于包装组件的用户不是很有用。它丢弃有关组件接受什么道具的信息。
为此,React类型提供了一个方便的类型
React.ComponentClass。它是类的类型,而不是从该类创建的组件的类型:
React.ComponentClass<Props>
(请注意,该
state类型是内部细节,因此未提及)。
在您的情况下:
export default function wrapperFunc <Props, State, CompState> ( WrappedComponent: typeof React.Component,): React.ComponentClass<Props & State> { return class extends React.Component<Props & State, CompState> { public render() { return <WrappedComponent {...this.props} {...this.state} />; } };}但是,您在使用
WrappedComponent参数做相同的事情。根据您在内部使用它的方式
render,我猜测还应该声明它:
WrappedComponent: React.ComponentClass<Props & State>,
但这是一个大胆的猜测,因为我认为这不是完整的功能(
CompState未使用,并且
Props &State可能是单个类型参数,因为它始终以该组合形式出现)。



