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;
Here
<Component {...props} firebase={firebase} />
Throws the below typescript error
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"> is added to avoid the required prop error in the Wrapped component import.
like
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);
I have casted the spread props as below
<Component {...(props as Props)} firebase={firebase} />}
But I am not sure if I am doing it right. Could you please let me know if there are any other way to tackle this error?