Categories
TypeScript Answers

How to fix Property ‘click’ does not exist on type ‘Element’ with TypeScript?

Spread the love

Sometimes, we want to fix Property ‘click’ does not exist on type ‘Element’ with TypeScript.

In this article, we’ll look at how to fix Property ‘click’ does not exist on type ‘Element’ with TypeScript.

How to fix Property ‘click’ does not exist on type ‘Element’ with TypeScript?

To fix Property ‘click’ does not exist on type ‘Element’ with TypeScript, we cast the element as a HTMLElement.

For instance, we write

const [element] = document.getElementsByClassName("btn");
(element as HTMLElement).click();

to select the elements with class btn with getElementsByClassName.

Then we get the element and cast it to an HTMLElement with as.

Finally, we call click on it to click it.

Conclusion

To fix Property ‘click’ does not exist on type ‘Element’ with TypeScript, we cast the element as a HTMLElement.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *