Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

2K
Visualizações
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 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

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 Relatório

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 Relatório

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda