const withFirebase = <Props extends {firebase: Firebase}>(
Component: React.ComponentType<Props>
): ComponentType<Omit<Props, "firebase">> => (props) => (
<FirebaseContext.Consumer>
{(firebase) => <Component {...props} firebase={firebase} />}
</FirebaseContext.Consumer>
);
export default withFirebase;
这里
<Component {...props} firebase={firebase} />
抛出以下typescript错误
Type 'Pick<Props, Exclude<keyof Props, "firebase">> & { firebase: Firebase | null; children?: ReactNode; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
Type 'Pick<Props, Exclude<keyof Props, "firebase">> & { firebase: Firebase | null; children?: ReactNode; }' is not assignable to type 'Props'.
'Props' could be instantiated with an arbitrary type which could be unrelated to 'Pick<Props, Exclude<keyof Props, "firebase">> & { firebase: Firebase | null; children?: ReactNode; }
添加了 Omit<Props, "firebase"> 以避免在 Wrapped 组件导入中出现所需的 prop 错误。
喜欢
const App = () => {
return (
<ItemWithFirebase /> // If not omit is available this will throw prop firebase not available error
)
}
const Item: FC<{firebase: Firebase}> = ({firebase}) => {
...
}
const ItemWithFirebase = withFirebase(Item);
我已经铸造了如下传播props
<Component {...(props as Props)} firebase={firebase} />}
但我不确定我是否做得对。您能否让我知道是否有其他方法可以解决此错误?