You're using useRef and TypeScript is yelling at you:
const inputRef = useRef(null);
if (inputRef.current) {
inputRef.current.value; // Property 'value' does not exist on type 'never'
}
That type 'never' is confusing. Let's fix it.
Why it happens
useRef(null) infers the type as RefObject<null> (MutableRefObject<null> before React 19), which means current can only be null. The if (inputRef.current) check removes null, and nothing is left, so TypeScript narrows current to never. Without the check you get 'inputRef.current' is possibly 'null' instead.
The fix
Option 1: Provide a type argument
const inputRef = useRef<HTMLInputElement>(null);
inputRef.current?.value; // Works
inputRef.current?.focus(); // Also works
current is now HTMLInputElement | null, so you still need ?. or an if check. It's null until React attaches the element.
Don't swap null for undefined here. useRef<HTMLInputElement | undefined>(undefined) fails as soon as you pass it to <input ref={inputRef} />, because React's ref prop expects HTMLInputElement | null.
Option 2: Read the ref in a useEffect
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
For callback refs
const inputRef = useRef<HTMLInputElement | null>(null);
const setInputRef = (element: HTMLInputElement | null) => {
inputRef.current = element;
};
return <input ref={setInputRef} />;
React calls the callback with null when the element unmounts, so type the parameter as HTMLInputElement | null.
Getting a parsing error in a .jsx file?
useRef<HTMLInputElement>(null) is TypeScript syntax. In a .js or .jsx file, ESLint and Babel read <HTMLInputElement> as JSX and report a parsing error. Either rename the file to .tsx, or keep it as JavaScript and put the type in a JSDoc comment:
/** @type {import('react').RefObject<HTMLInputElement | null>} */
const inputRef = useRef(null);
With checkJs on, TypeScript reads that comment and types inputRef.current the same way as in the .tsx version.
TL;DR
Add the type argument:
const ref = useRef<HTMLInputElement>(null);
That's it. useRef<HTMLElement>(null) tells TypeScript what type the ref will hold.