I\'ve got the \"Object is possibly null\" error many times and usually I use a safety \"if statement\" in case it returns null.
I\'ve got the following function:
<
Add a type to the ref as mentioned by @Shanon Jackson:
const linkRef = useRef(null);
And then, make sure you check for null
value before using current
:
if (linkRef.current !== null) {
linkRef.current.focus();
}
This will satisfy Typescript. Whereas either by itself wouldn't.
Using any
or casting in order to "trick" the compiler defeats the purpose of using Typescript, don't do that.