forethought
forethought

Reputation: 3253

TypeScript complains about HTMLElement do not have value property

TypeScript complains about HTMLElement do not have value property but when I use it in JavaScript it works fine.

 var inputValue: HTMLElement = document.getElementById('input1');
 console.log(inputValue.value); // show error message

In JavaScript, var inputValue= document.getElementById('input1'); console.log(inputValue.value); // it gives me the value of input element

Can I know why TypeScript complains?

Upvotes: 0

Views: 2636

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164129

HTMLElement doesn't have the value member, the HTMLInputElement does.
You need to type assert it:

var inputValue = document.getElementById('input1') as HTMLInputElement;
console.log(inputValue.value); // should be ok

Edit

The typescript definitions represent actual javascript dom elements, in this case for HTMLElement and HTMLInputElement.

Upvotes: 6

Related Questions