我有一个用React.forwardRef
包装的组件,碰巧它也是一个复合组件。
我不确定如何维护
Form.Item = FormItem;
,而Form组件函数用forwardRef
包装。
const Form = React.forwardRef((props, ref) => { return <form></form>});
打字稿给我一个错误(严重)是
类型为'ForwardRefExoticComponent>'的属性'Item'不存在。ts(2339)
答案 0 :(得分:1)
只是为了更清楚地说明解决方案,因为它位于外部网站上(感谢原作者 here,感谢@Antoan Elenkov)
export interface Props = {
...yourPropsHere;
};
export interface CompoundedComponent extends React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLInputElement>> {
yourStaticFunctionOrSomethingLikeThat: () => void;
}
const Component = React.forwardRef<HTMLInputElement, Props>((props, ref) => (
<input ref={ref} {...props} />
)) as CompoundedComponent;
Component.yourStaticFunctionOrSomethingLikeThat = () => {};