I have React HOC which restricts component visibility based on fixed prop
const RenderWhenIsVisible = Component => {
return function (props) {
let { isVisible, ...newProps } = props;
return props.isVisible ? <Component {...newProps} /> : null;
};
};
When try to rewrite to TS errors appear. Below is the version with lowest errors number, in the meantime I have passed through few others
interface isVisible {
isVisible: boolean;
}
function RenderWhenIsVisible<T>(Component: React.ComponentType<T>) {
return function (props: T & isVisible ) {
let { isVisible, ...newProps} = props;
return props.isVisible ? <Component {...newProps } /> : null;
};
}
The error is:
Type 'Omit<T & isVisible, "isVisible">' is not assignable to type 'IntrinsicAttributes & T & { children?: ReactNode; }'. Type 'Omit<T & isVisible, "isVisible">' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'Omit<T & isVisible, "isVisible">'.
I understand that ...newProps shoud be clearly typed as T but have no idea how to pass such the declaration.