0

I'm using the following code for reading in contents of a locally stored file.

onFile(event: any) {
  console.log(event);
  const file = event.target.files[0];
  const reader = new FileReader();
  reader.onloadend = (ev: any) => { console.log(ev); };
  reader.readAsText(file);
}

Investigating the two console outputs, I see that the two types printed out are Event and ProgressEvent. So I've refactored my methods to correspond the types of the parameters like so.

onFile(event: Event) {
  console.log(event);
  const file = event.target.files[0];
  const reader = new FileReader();
  reader.onloadend = (ev: ProgressEvent) => { console.log(ev, $event.target.result); };
  reader.readAsText(file);
}

However, although I've done that, I still see TsLint nag about files[0] and result not present in their types. Have I specified incorrect type for the operations? What is the appropriate type in such case?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

0

target.files is only a property on an input event's target. The compiler does not know what type of event you are passing to it and there is no event of type input that you can give there as an object type. You either need to create one yourself or leave it as any. => https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

Ivan Mihaylov
  • 434
  • 2
  • 9