I just can't get the typings right. I am forwarding a ref which works correctly if I set ref: any as shown below and ref.current.focus() works.
function EditorFeat(): JSX.Element {
const ref = useRef<HTMLElement | null>(null);
// ...
return (
......
<Editor state={state} dispatch={dispatch} ref={ref} />
....
)
}
export default EditorFeat;
interface IEditor {
state: AppState;
dispatch: React.Dispatch<Action>;
}
// change ref:any to correct typings!
function Editor({ state, dispatch }: IEditor, ref: any): JSX.Element {
// ...
return <DraftJsEditor ref={ref} /> // import from 'draft-js'
}
export default React.forwardRef(Editor);
but as soon as I try to get ref type right, I keep getting errors!
if I do
function Editor({state, dispatch}: IEditor, ref: HTMLElement): JSX.Element
I get next error:
Overload 1 of 2, '(props: Readonly<DraftEditorProps>): DraftEditor', gave the following error.
Type 'HTMLElement' is not assignable to type 'string | ((instance: DraftEditor | null) => void) | RefObject<DraftEditor> | null | undefined'.
Type 'HTMLElement' is not assignable to type 'string'.
Overload 2 of 2, '(props: DraftEditorProps, context?: any): DraftEditor', gave the following error.
Type 'HTMLElement' is not assignable to type 'string | ((instance: DraftEditor | null) => void) | RefObject<DraftEditor> | null | undefined'.
Type 'HTMLElement' is not assignable to type 'string'
if I try React.RefObject<DraftEditor> I get:
Argument of type '({ state, dispatch }: IEditor, ref: RefObject<DraftEditor>) => Element' is not assignable to parameter of type 'ForwardRefRenderFunction<DraftEditor, IEditor>'.
Types of parameters 'ref' and 'ref' are incompatible.
Type '((instance: DraftEditor | null) => void) | MutableRefObject<DraftEditor | null> | null' is not assignable to type 'RefObject<DraftEditor>'.
Type 'null' is not assignable to type 'RefObject<DraftEditor>'