Typescript not enabling the use of the files property

Asked

Viewed 114 times

0

I’m having a problem in Ionic, where I use the property .files in an id of an element <input type="file">, in that way:

<input type="file" id="images" multiple>
this.register.images = document.getElementById('images').files;

And it works, the this.register.images is populated with files sent to input however, the Text Editor I use (Visual Studio Code) points out the following error: inserir a descrição da imagem aqui

Which in turn, when I build Ionic, it causes me an error in the compilation: inserir a descrição da imagem aqui

And so, I can’t continue with the application development, so:

  • Is there any way to make this mistake go away?
  • Or some other way to catch the files of a <input type="file">?

1 answer

0


It is not an error, what happens is that the method document.getElementById returns something like this HTMLElement | null, therefore Voce has to check the instance of its element.

let element: HTMLElement | null = document.getElementById('images');
if ( element instanceof HTMLInputElement ) {
    this.register.images = element.files
}

This is because Typescript has no way of knowing the type of element that will be present in the DOM, so it returns the interface that meets the need for any element( span, div, p, etc...)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.