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

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.