Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2K
Views
What Typescript type is a change event? (In Angular)

I'm trying to figure out what Typescript type a change event is used in an Angular project.

The code is something simple like this:

file-upload.component.html

<input type="file" (change)="onChange($event)"/>

file-upload.ts

public onChange(event: Event): void {
  if (event.target.files && event.target.files.length) {
    const [file] = event.target.files;
    console.log(file);
  }
}

Typing the event as Event gives me the following Typescript linting error:

Property 'files' does not exist on type 'EventTarget'

What should I be typing this if not Event?

over 4 years ago · Santiago Trujillo
6 answers
Answer question

0

the files should be a accessible thru the event.srcElement.files property.

over 4 years ago · Santiago Trujillo Report

0

It is an event. But you're going to have to cast the const you're using as an HTMLInputElement.

public onChange(event: Event): void {
  if ((event.target as HTMLInputElement).files && (event.target as HTMLInputElement).files.length) {
    const [file] = event.target.files;
  }
}

The only other way is going to be to suppress the error with tsignore. In react, flow, they have a type SyntheticEvent that you can type this particular case as to get around it but angular doesn't have a real equivalent.

over 4 years ago · Santiago Trujillo Report

0

public onChange(event: Event): void {
    const input = event.target as HTMLInputElement;

    if (!input.files?.length) {
        return;
    }

    const file = input.files[0];
    console.log(file);
}
over 4 years ago · Santiago Trujillo Report

0

You just have to write this before doing anything with your event:

if (!(event.target instanceof HTMLInputElement)) return;

Typescript will then know that your event.target is an instance of HTMLInputElement and it will stop yelling at you.

over 4 years ago · Santiago Trujillo Report

0

You could specify property target of Event to be an HTMLInputElement using an intersection type as the typehint of event.

public onChange(event: Event & { target: HTMLInputElement }): void {
  if (event.target.files && event.target.files.length) {
    const [file] = event.target.files;
    console.log(file);
  }
}

An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has all the features you need.

https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#intersection-types

over 4 years ago · Santiago Trujillo Report

0

The accepted answer throws two errors in my compiler: (event.target as HTMLInputElement).files is possibly 'null' and Type 'FileList' must have a '[Symbol.iterator]()' method that returns an iterator.

This works for me:

const target = event.target as HTMLInputElement;

if (target.files && target.files.length) {
  const file = target.files[0];
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!