Borislav Hadzhiev
Fri Mar 18 2022·2 min read
Photo by Mohammad Behrooz
The error "Property 'value' does not exist on type 'HTMLElement'" occurs when
we try to access the value
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.
This is the index.html
file for the examples in this article.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <input id="first_name" type="text" name="first_name" value="Initial Value" /> <script src="./src/index.ts"></script> </body> </html>
And here is an example of how the error occurs in the index.ts
file.
// 👇️ const input: HTMLElement | null const input = document.getElementById('first_name'); if (input != null) { // ⛔️ Error: Property 'value' does not exist on type 'HTMLElement'.ts(2339) const value = input.value; }
The reason we got the error is because the return type of the
document.getElementById
method is HTMLElement | null
and the value
property doesn't exist in the
HTMLElement
type.
To solve the error, use a type assertion to type the element as an
HTMLInputElement
(or HTMLTextAreaElement
if you're typing a textarea
element).
const input = document.getElementById('first_name') as HTMLInputElement | null; if (input != null) { const value = input.value; console.log(value); // 👉️ "Initial value" }
Type assertions are used when we have information about the type of a value that TypeScript can't know about.
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 value
property.
const input = document.getElementById('first_name') as HTMLInputElement | null; // 👉️ input has type HTMLInputElement or null here if (input != null) { // 👉️ input has type HTMLInputElement here const value = input.value; console.log(value); // 👉️ "Initial value" }
input
variable has a type of HTMLInputElement
in the if
block and allows us to directly access its value
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.
You might also use the
optional chaining (?.)
operator to short-circuit if the reference is equal to null
or undefined
const input = document.getElementById('first_name') as HTMLInputElement | null; // 👇️ using optional chaining (?.) const value = input?.value; console.log(value); // 👉️ Initial value
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 access the value
property on null
and get a runtime error.