The following should work, but errors
function foo() {
const inputRef = useRef();
return <input type="text" defaultValue="a default" ref={inputRef}/>
}
WARNING in [at-loader] ./src/App.tsx:25:65
TS2322: Type 'MutableRefObject<undefined>' is not assignable to type 'string | ((instance: HTMLInputElement | null) => void) | RefObject<HTMLInputElement> | null | undefined'.
Type 'MutableRefObject<undefined>' is not assignable to type 'RefObject<HTMLInputElement>'.
Types of property 'current' are incompatible.
Type 'undefined' is not assignable to type 'HTMLInputElement | null'.
Example: https://reactjs.org/docs/hooks-reference.html#useref
@types/xxxx package and had problems.Definitions by: in index.d.ts) so they can respond.For anyone reading this, I should have used useRef<HTMLInputElement>(null), this fixes the problem
Expected types:
string((instance: HTMLInputElement | null) => void)RefObject<HTMLInputElement>nullundefinedPassed in type
MutableRefObject<HTMLInputElement | null>Still don't understand why these two can match?
Most helpful comment
For anyone reading this, I should have used
useRef<HTMLInputElement>(null), this fixes the problem