Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339

In my component, I have the following code to scroll to the bottom of the page:

const el = useRef(null);

useEffect(() => {
    if (el.current === null) { }
    else
        el!.current!.scrollIntoView({ block: 'end', behavior: 'smooth' });

    if (props.loading != true)
        props.fetchFeedbackTaskPageData(submissionId);

}, [])

This el ref is attached to a div (at the bottom of the page) like this:

<div id={'el'} ref={el}></div>

However, I am receiving the following error:

Property 'scrollIntoView' does not exist on type 'never'. TS2339

When I was not using !, I was receiving this error:

Object is possibly 'null'. TS2531

I checked many posts on this issue but did not see how to handle this in react when using useRef and scrollIntoView. Any ideas?

like image 500
renakre Avatar asked Jul 14 '19 14:07

renakre


People also ask

How do you fix property does not exist on type never?

The error "Property does not exist on type 'never'" occurs when we forget to type a state array or don't type the return value of the useRef hook. To solve the error, use a generic to explicitly type the state array or the ref value in your React application.

Does not exist on type never TypeScript?

The error "Property does not exist on type 'never'" occurs when we try to access a property on a value of type never or when TypeScript gets confused when analyzing our code. To solve the error, use square brackets to access the property, e.g. employee['salary'] .


1 Answers

For anyone reading this in 2020 its

const el = useRef<null | HTMLDivElement>(null); 

and no longer:

const el = useRef<null | HTMLElement>(null);
like image 197
Arsh Sharma Avatar answered Sep 19 '22 10:09

Arsh Sharma