Property 'checked' does not exist on type 'HTMLElement'

avatar

Borislav Hadzhiev

Last updated: Mar 29, 2022

banner

Photo from Unsplash

Property 'checked' does not exist on type 'HTMLElement' #

The error "Property 'checked' does not exist on type 'HTMLElement'" occurs when we try to access the checked property on an element that has a type of HTMLElement. To solve the error, use a type assertion to type the element as HTMLInputElement before accessing the property.

property checked not exist type htmlelement

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="checkbox" name="subscribe" id="subscribe" /> <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: HTMLElement | null const input = document.getElementById('subscribe'); if (input != null) { // ⛔️ Error: Property 'checked' does not exist on type 'HTMLElement'.ts(2339) input.checked = true; }

The reason we got the error is that the return type of the document.getElementById method is HTMLElement | null and the checked property doesn't exist in the HTMLElement type.

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

src/index.ts
const input = document.getElementById('subscribe') as HTMLInputElement | null; if (input != null) { input.checked = true; }

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 anHTMLInputElement 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 id does not exist in the DOM, the getElementById() method returns a null value.

We used a simple if statement that serves as a type guard to make sure the input variable doesn't store a null value before accessing its checked property.

src/index.ts
const input = document.getElementById('subscribe') as HTMLInputElement | null; // 👉️ input has type HTMLInputElement or null here if (input != null) { // 👉️ input has type HTMLInputElement here input.checked = true; }

We explicitly check that the input variable does not store a null value.

TypeScript knows that the input variable has a type of HTMLInputElement in the if block and allows us to directly access its checked property.

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

Using type assertions to specify an element's type is something you will commonly have to do when writing client-side code in TypeScript.

Some commonly used types are: HTMLInputElement, HTMLButtonElement, HTMLAnchorElement, HTMLImageElement , HTMLTextAreaElement, HTMLDivElement etc.

The types of DOM elements are consistently named HTML***Element. Once you start typing HTML.., your IDE should be able to help you with autocomplete.

Conclusion #

The error "Property 'checked' does not exist on type 'HTMLElement'" occurs when we try to access the checked property on an element that has a type of HTMLElement. To solve the error, use a type assertion to type the element as HTMLInputElement before accessing the property.

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.