Property 'files' does not exist on type 'HTMLElement' in TS

avatar

Borislav Hadzhiev

Sun Mar 20 20222 min read

banner

Photo by Hiva Sharifi

Property 'files' does not exist on type 'HTMLElement' in TS #

The error "Property 'files' does not exist on type 'HTMLElement'" occurs when we try to access the files 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 files.

property files 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> <label for="avatar">Choose a profile picture:</label> <input type="file" id="avatar" name="avatar" accept="image/png, image/jpeg, image/webp" /> <button id="btn">Log files</button> <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('avatar'); const btn = document.getElementById('btn'); btn?.addEventListener('click', () => { // ⛔️ Property 'files' does not exist on type 'HTMLElement'.ts(2339) const files = input?.files; console.log(files); });

The reason we got the error is because the return type of the document.getElementById method is HTMLElement | null and the files 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('avatar') as HTMLInputElement | null; const btn = document.getElementById('btn'); btn?.addEventListener('click', () => { const files = input?.files; console.log(files); });

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 the optional chaining (?.) operator to get around the possibly null value in the example.

src/index.ts
const input = document.getElementById('avatar') as HTMLInputElement | null; const btn = document.getElementById('btn'); // 👇️ optional chaining (?.) btn?.addEventListener('click', () => { // 👇️ optional chaining (?.) const files = input?.files; console.log(files); });

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 files property on null and get a runtime error.

Alternatively, you can use a simple if statement that serves as a type guard.

src/index.ts
const input = document.getElementById('avatar') as HTMLInputElement | null; const btn = document.getElementById('btn'); if (btn != null) { btn.addEventListener('click', () => { // 👉️ input has type HTMLInputElement or null here if (input != null) { // 👉️ input has type HTMLInputElement here const files = input.files; console.log(files); } }); }

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 the files property.

Which approach you pick to exclude null from the type before accessing the files property is a matter of personal preference.

However, 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.

Use the search field on my Home Page to filter through my more than 1,000 articles.