Angular – Property ‘files’ does not exist on type ‘EventTarget’ error in typescript

angularexif-jsionic2typescript

I am trying to access the value of the input file from my ionic 2 application but still I'm facing the issue of property files does not exist on type 'EventTarget'.
As it is properly working in js but not in typescript.
The code is given below:

  document.getElementById("customimage").onchange= function(e?) {
            var files: any = e.target.files[0]; 
              EXIF.getData(e.target.files[0], function() {
                  alert(EXIF.getTag(this,"GPSLatitude"));
              });
          }

Please help me solve this issue as it is not building my ionic 2 application.

Best Answer

You can cast it as a HTMLInputElement:

document.getElementById("customimage").onchange = function(e: Event) {
    let file = (<HTMLInputElement>e.target).files[0];
    // rest of your code...
}

Update:

You can also use this:

let file = (e.target as HTMLInputElement).files[0];