I'm struggling to understand why the typescript compiler is unhappy with the forwardRef I'm using in a component.
I've pared my code right down to the bare essentials for the example below, ensuring this typescript error is the only one that manifests in the code, as per the issue in my actual code:
import { createRef, forwardRef, RefObject } from 'react';
const MyComponent = () => {
const selectCountryRef = createRef<HTMLSelectElement>();
return (
<div>
<SelectCountry ref={selectCountryRef} />
</div>
);
};
const SelectCountry = forwardRef((ref?: RefObject<HTMLSelectElement>) => {
return (
<FormFieldSelect ref={ref} />
);
});
const FormFieldSelect = ({ ref }) => {
return (
<div>
<select ref={ref}>
</select>
</div>
);
};
When trying to compile this, I get the error:
Property 'current' is missing in type '{ ref: RefObject<HTMLSelectElement>; }' but required in type 'RefObject<HTMLSelectElement>'.
In VSCode, <SelectCountry ... is where the IDE is having a whinge, the exact message is:
const SelectCountry: ForwardRefExoticComponent<RefObject<HTMLSelectElement> & RefAttributes<unknown>> Property 'current' is missing in type '{ ref: RefObject<HTMLSelectElement>; }' but required in type 'RefObject<HTMLSelectElement>'.ts(2741) index.d.ts(85, 18): 'current' is declared here.
I'm just failing to understand why I'm getting this error.
Currently using React 17.0.2