I have this Higher order component written down:
type WrappedComponentConditionallyHidden =
<P>(WrappedComponent: React.ComponentType<P>) => React.FC<P & { isHidden: boolean }>;
const hideConditional: WrappedComponentConditionallyHidden = (WrappedComponent) => {
return (props) => {
// typeof props is inferred here correctly: P & { isHidden: boolean }
// Don't want to pass down "isHidden"
const { isHidden, ...passedProps } = props;
if (props.isHidden) {
return null;
} else {
return <WrappedComponent {...passedProps} />
// Passing { ...props } works but ^^^^ this fails
}
}
}
I get this Typescript error:
Type 'Pick<PropsWithChildren<P & { isHidden: boolean; }>, "children" | Exclude<keyof P, "isHidden">>' is not assignable to type 'IntrinsicAttributes & P & { children?: ReactNode; }'
Type 'Pick<PropsWithChildren<P & { isHidden: boolean; }>, "children" | Exclude<keyof P, "isHidden">>' is not assignable to type 'P'.
'Pick<PropsWithChildren<P & { isHidden: boolean; }>, "children" | Exclude<keyof P, "isHidden">>' is assignable to the constraint of type 'P', but 'P' could be instantiated with a different subtype of constraint '{}'.
For me, 'Pick<PropsWithChildren<P & { isHidden: boolean; }>, "children" | Exclude<keyof P, "isHidden">>
should be equal to PropsWithChildren<P>
and hence it should work. Any ideas why it wouldn't?
} = props;`. At the very least you might get a more helpful compiler error telling you why the spread variable can't be cast.
– JDB Jun 15 '21 at 16:30