Skip to main content

add useRef() with types

const inputRef = useRef()

return <input ref={inputRef} /\>
Type 'MutableRefObject<undefined\>' is not assignable to type 'LegacyRef<HTMLInputElement\> | 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'.ts(2322)

Fix by add <HTMLInputElement>

const inputRef = useRef<HTMLInputElement | null\>(null);

alt+click HTMLInputElement to see other HTML element types - assign relevant element type depending on where ref is attached

TS Redux

References