error TS2345: Argument of type '(props: WithoutTheme<P>, ref: any) => Element' is not assignable to parameter of type 'ForwardRefRenderFunction<unknown, PropsWithoutRef<P>>'.
Types of parameters 'props' and 'props' are incompatible.
Type 'PropsWithoutRef<P>' is not assignable to type 'WithoutTheme<P>'.
Type '{ theme?: Theme | undefined; }' is not assignable to type 'WithoutTheme<P>'.
return forwardRef(function ComponentWithTheme(
Omit<P, "theme">
进行修改,这在此引起问题。修复此
function withTheme<P extends { theme?: Theme }, R>(
Component: ComponentType<P & React.RefAttributes<R>>
) {
return forwardRef<R, WithoutTheme<P>>(function ComponentWithTheme(
props,
ref
) {
const theme = useTheme();
const combinedProps = { ...props, theme } as P;
return <Component ref={ref} {...combinedProps} />;
});
}
包装的组件可以正确键入Ref(R),并且ForwardRef适当地注入道具和参考文献