Categories
TypeScript Answers

How to fix “The property ‘value’ does not exist on value of type ‘HTMLElement'” error with TypeScript?

Spread the love

Sometimes, we want to fix "The property ‘value’ does not exist on value of type ‘HTMLElement’" error with TypeScript.

In this article, we’ll look at how to fix "The property ‘value’ does not exist on value of type ‘HTMLElement’" error with TypeScript.

How to fix "The property ‘value’ does not exist on value of type ‘HTMLElement’" error with TypeScript?

To fix "The property ‘value’ does not exist on value of type ‘HTMLElement’" error with TypeScript, we should cast our HTML object to a HTMLInputElement.

For instance, we write

const inputValue = (<HTMLInputElement>document.getElementById(elementId)).value;

to cast the object returned by document.getElementById(elementId) to an HTMLElementInput.

Likewise, we can do the same with as by writing

const inputValue = (document.getElementById(elementId) as HTMLInputElement)
  .value;

Conclusion

To fix "The property ‘value’ does not exist on value of type ‘HTMLElement’" error with TypeScript, we should cast our HTML object to a HTMLInputElement.

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 *