【发布时间】:2020-05-01 15:17:42
【问题描述】:
我正在尝试在 React Native 应用程序中创建一个 HOC,但在使定义正常工作时遇到了问题。
interface IWithApolloClientComponentProps {
apolloClient: ApolloClient<any>;
}
export function withApolloClient<
P extends IWithApolloClientComponentProps,
R = Omit<P, keyof IWithApolloClientComponentProps>
>(
Component: React.ComponentType<P>
): React.ComponentType<R> {
return function BoundComponent(props: R) {
return <ApolloConsumer>{client => <Component {...props} apolloClient={client} />} .
</ApolloConsumer>;
};
}
我的 VSCode 一直报以下错误:
Type 'R & { apolloClient: ApolloClient<object>; }' is not assignable to type 'IntrinsicAttributes & P & { children?: ReactNode; }'.
Type 'R & { apolloClient: ApolloClient<object>; }' is not assignable to type 'P'.
'R & { apolloClient: ApolloClient<object>; }' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint 'IWithApolloClientComponentProps'.ts(2322)
有人看到我在这里做错了什么吗?
【问题讨论】:
-
HOC 是代表“高阶计算器”还是其他什么?
标签: typescript react-native apollo-client