我想获取具有其prop类型的组件,但是如果我从嵌套函数返回组件,Typescript无法推断它们。
我试图从一个未嵌套的函数中返回该组件,它可以工作,但不能从一个嵌套的函数中返回
我希望从getComponentFromNestedFunction获取ComponentType<ICompProps>
点击以下链接以查看完整的链接:code example
interface ICompProps {
pass?: string;
user?: string;
}
function getComponentFromNestedFunction<Props>(
func: () => void
): (Comp: ComponentType<Props>) => ComponentType<Props> {
return Comp => Comp;
}
function getComponent<Props>(
Comp: ComponentType<Props>
): ComponentType<Props> {
return Comp;
}
const Comp: ComponentType<ICompProps> = p => {
return <div>{p.user}</div>;
};
// How can I change the code so I can get the prop types like in ComponentWithPropTypes?
const ComponentWithoutPropTypes = getComponentFromNestedFunction(() => ({
user: "user"
}))(Comp);
// hover (in a code editor) ComponentWithPropTypes and see the prop types: const ComponentWithPropTypes: React.ComponentType<ICompProps>
const ComponentWithPropTypes = getComponent(Comp);
}