Property ‘value’ Does Not Exist On Type ‘HTMLElement’ In TypeScript – How to fix?

Property ‘value’ does not exist on type ‘HTMLElement’ in TS

You accessed the ‘value’ property of HTMLElement then TypeScript returned an error message: ‘The property ‘value’ does not exist on type ‘HTMLElement’. So why did it happen, and how can we fix it? You can find the answer in this article.

The cause of the error

Trying to access the ‘value’ property of an element that type is ‘HTMLElement’ and expect to get the value of that element. However, TypeScript returns an error message like this:

ERR: "Property 'value' does not exist on type 'HTMLElement'"

This problem occurs because the ‘value’ property does not exist in ‘HTMLElement’ type.

Let’s take a look at the example below:

Here is your ‘infor.html’ file:

<!DOCTYPE html>
<html>
<head>
  	<meta charset="UTF-8"/>
</head>
<body>
    <input id="jobTitle" type="text" value="Job position"/>
    <input id="contact" type="text" value="Email or Phonenumber"/>
    <button id="btn">Submit</button>
    <script src="./src/index.ts"></script>
</body>
</html>

Suppose we access the ’value’ property with the following statements in ‘index.ts’:

index.ts:

const jobTitle = document.getElementById('jobTitle');
const contact = document.getElementById('contact');

console.log(jobTitle.value)
console.log(contact.value)

Error message:

ERR: "Property 'value' does not exist on type 'HTMLElement'"
ERR: "Property 'value' does not exist on type 'HTMLElement'"

Of course, we get errors message because these commands are not valid.

Solutions to fix “Property ‘value’ does not exist on type ‘HTMLElement’” error in TypeScript

So the question is, how do we get the value of that element?

To fix this problem we use type casting that cast ‘HTMLElement’ to ‘HTMLInputElement’.

Edit your ‘index.ts’ as follows:

index.ts:

const jobTitle = document.getElementById('jobTitle') as HTMLInputElement;
const contact = document.getElementById('contact') as HTMLInputElement;

console.log(jobTitle.value)
console.log(contact.value)

Output:

Job position
Email or Phonenumber

Here you can see the output is the values we need just by using the as keyword to make a simple command.

You can also use the <> operator to get the same result as above. For example:

const jobTitle = (document.getElementById('jobTitle')).value;
const contact = (document.getElementById('contact')).value;

console.log(jobTitle.value)
console.log(contact.value)

Output:

Job position
Email or Phonenumber

In addition, you should use conditional statements to ensure your element is not null before access to its property.

const jobTitle = (<htmlinputelement>document.getElementById('jobTitle')).value;
const contact = (<htmlinputelement>document.getElementById('contact')).value;
const nickname = (<htmlinputelement>document.getElementById('nickname')).value;

let myInputs = [jobTitle, contact, nickname]

for (var inp of myInputs) {
	if (inp != null) {
    	console.log(inp)
    }
}

console.log(jobTitle.value)
console.log(contact.value)

Output:

Job position
Email or Phonenumber

*Explain: The ‘nickname’ element returns a null value due to no element with id as ‘nickname’. So we only have two output values.

Summary

You can use either <> operator or as keyword to cast the ‘HTMLElement’ type to ‘HTMLInputElement’ type then access the property ‘value’. This method will have you solve the “Property ‘value’ does not exist on type ‘HTMLElement'” error in TypeScript easily.

Maybe you are interested:

Leave a Reply

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