Specify the element type for refs.
Section: TypeScript with State and Refs
Type a DOM ref
tsx
tsx
const inputRef = useRef<HTMLInputElement | null>(null);Explanation
Typing refs correctly improves DOM API autocomplete and safety.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing TypeScript with State and Refs
Discriminated union reducer actions
Type reducer actions cleanly with a union.
Typed tuple return
Return readonly tuples for hooks that act like useState.