Property 'focus' does not exist on type 'Element' in TS

avatar

Borislav Hadzhiev

Last updated: Mar 25, 2022

banner

Photo from Unsplash

Property 'focus' does not exist on type 'Element' in TS #

The error "Property 'focus' does not exist on type 'Element'" occurs when we try to call the focus() method on an element that has a type of Element. To solve the error, use a type assertion to type the element as HTMLElement before calling the method.

property focus not exist type element

This is the index.html file for the examples in this article.

index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <input type="text" id="first_name" /> <script src="./src/index.ts"></script> </body> </html>

And here is an example of how the error occurs in the index.ts file.

src/index.ts
// 👇️ const input: Element | null const input = document.querySelector('#first_name'); if (input != null) { // ⛔️ Property 'focus' does not exist on type 'Element'.ts(2339) input.focus(); }

The reason we got the error is that the return type of the document.querySelector method is Element | null and the focus method doesn't exist on the Element type.

To solve the error, use a type assertion to type the element as an HTMLElement.

src/index.ts
const input = document.querySelector('#first_name') as HTMLElement | null; if (input != null) { input.focus(); }

If you used the document.getElementsByClassName method, type the collection as HTMLCollectionOf<HTMLElement>.

src/index.ts
// 👇️ with getElementsByClassName // type as HTMLCollectionOf<HTMLElement> const inputs = document.getElementsByClassName( 'input', ) as HTMLCollectionOf<HTMLElement>; for (let i = 0; i < inputs.length; i++) { inputs[i].focus(); }

We used the HTMLElement type in the example above, however we could have also used the HTMLInputElement type to be more specific.

Type assertions are used when we have information about the type of a value that TypeScript can't know about.

We effectively tell TypeScript that the input variable stores anHTMLElement or a null value and not to worry about it.

We used a union type to specify that the variable could still be null, because if an HTML element with the provided selector does not exist in the DOM, the querySelector() method returns a null value.

src/index.ts
const input = document.querySelector('#first_name') as HTMLElement | null; // 👉️ input has type HTMLElement or null here if (input != null) { // 👉️ input has type HTMLElement here input.focus(); }
TypeScript knows that the input variable has a type of HTMLElement in the if block and allows us to directly call its focus() method.

It's always a best practice to include null in the type assertion because the querySelector method would return null if no element with the provided selector was found.

You might also use the optional chaining (?.) operator to short-circuit if the reference is equal to null or undefined

src/index.ts
const input = document.querySelector('#first_name') as HTMLElement | null; // 👇️ using optional chaining (?.) input?.focus();

The optional chaining operator short-circuits returning undefined if the reference is equal to null or undefined.

In other words, if the input variable stores a null value, we won't attempt to call the focus() method on null and get a runtime error.

Conclusion #

The error "Property 'focus' does not exist on type 'Element'" occurs when we try to call the focus() method on an element that has a type of Element. To solve the error, use a type assertion to type the element as HTMLElement before calling the method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.