这是我的代码,但不会通过打字稿验证
import * as React from "react";
export function makeWrapperFn<P, C extends React.ComponentType >(Comp: C, baseProps = {} ) {
return (props: Partial<P> = {} ) => {
// I want this returned function can override the props
return (<Comp {...baseProps} {...(props)} />);
// Comp is problem here
};
}
错误消息是:
Type 'Partial<P>' is not assignable to type 'IntrinsicAttributes & LibraryManagedAttributes<C, { children?: ReactNode; }>'.
Type 'Partial<P>' is not assignable to type 'LibraryManagedAttributes<C, { children?: ReactNode; }>'.ts(2322)
那么,如何使这种包装器函数制造商具有Typescript约束呢?
我的评论中的答案非常相似。
但是,有一个区别-没有Partial
道具。
因此,我决定回答,随时将其标记为重复。
import * as React from "react";
// If You want to use Partial, please use Partial also in type of Comp
export function makeWrapperFn<P extends object>(Comp: React.ComponentType<Partial<P>>, baseProps = {} ) {
return (props: Partial<P>) => {
// I want this returned function can override the props
return (<Comp {...baseProps} {...(props)} />);
// Comp is problem here
};
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句