# How To Fix The Error “Property ‘Files’ Does Not Exist On Type ‘EventTarget'” in TypeScript

This post discusses the error “Property ‘files’ does not exist on type ‘EventTarget'” in TypeScript. Let’s see what is causing the error as well as how we can solve this problem.

## Why do we get the error “Property ‘files’ does not exist on type ‘EventTarget'” in TypeScript

The error message “Property ‘files’ does not exist on type ‘EventTarget’” appears when accessing property ‘files‘ on the element of type ‘EventTarget‘.

To illustrate, let me reproduce the error case:

HTML snippet for this example:

<!DOCTYPE html>
<html>
<meta charset="UTF-8" />
<body>

<input type="file" id="picture" accept="image/png, image/jpeg" />

<button id="myButton">Confirm</button>

<script src="./main.ts"></script>
</body>
</html>

The error will occur in the ‘main.ts‘ file with the code below:

const myPicture = document.getElementById("picture");

console.log(event.target?.files);
});

Error:

As you can see, this error results from accessing the ‘files’ property on an element of type ‘EventTarget’, which does not have the ‘files’ property.

## The ways we can apply to solve this problem

To get around this problem, use the type assertion.

Type Assertion is a logic mechanism of TypeScript that allows you to set the type of a variable and tell TypeScript not to infer the type of the variable itself. Now we can manually manage the variable type or the type returned from a function.

Before accessing the element, we use type assertion to type it as ‘HTMLInputElement’

Take a look at this example, where we create a variable res to store the value of event.target. Then type the res into an HTMLInputElement.

const myPicture = document.getElementById("picture");

const res = event.target as HTMLInputElement;
console.log(res.files);
});

Besides, to make the code more concise, you can write on one line as follows:

const myPicture = document.getElementById("picture");

const res = (event.target as HTMLInputElement).files;
console.log(res);
});

At this point, TypeScript will understand that you are storing an HTMLInputElement in the variable res. That’s why you can access the ‘files’ property without worrying about an error.

A tip for you: Consistently named types are ‘HTML#Element’. When you type HTML.., the IDE will support you with autofill.

Some other popular types you can refer to: HTMLButtonElement, HTMLDivElement, HTMLFormElement, HTMLImageElement, HTMLOptionElement, etc.

## Summary

Finally, we have provided you with important information about the error “Property ‘files’ does not exist on type ‘EventTarget’ in TypeScript. Hopefully, this information will help you. That’s the end of this post. Thanks for reading.

Maybe you are interested: