Typescript: FileReader.onLoad/onLoadEnd event argument has no target.result property

Created on 5 Aug 2015  路  13Comments  路  Source: microsoft/TypeScript

Trying to retrieve the result of a read file/blob via the event passed into the reader's onload/onloadend method results in a type error (Property 'result' does not exist on type 'EventTarget').

This is valid JavaScript, which is failing type checking because the EventTarget interface does not have a result property.

Examples:

Workaround: Retrieve the result from the reader object (FileReader.result)

lib.d.ts Fixed Suggestion help wanted

Most helpful comment

Instead of using event.target.result, you can just use FileReader.result.

For example,

const fileReader: FileReader = new FileReader();

fileReader.onload = (event: Event) => {
   event.target.result; // This is invalid
   fileReader.result; // This is valid
};

All 13 comments

looks related to #299.

PRs welcomed. here is some infromation on contributing lib.d.ts changes: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes

This is used in other cases also such as IndexedDB onsuccess event.

My solution, until this all fixed up, has been just adding a union to the Event callback:

const reader = new FileReader();
reader.onload = (e: Event & { target: { result: string } }) => { ... }

It's gross, but does the trick. The better way would probably be something like this as it is way more complete and abstracts out the types, rather than inlining them.

Instead of using event.target.result, you can just use FileReader.result.

For example,

const fileReader: FileReader = new FileReader();

fileReader.onload = (event: Event) => {
   event.target.result; // This is invalid
   fileReader.result; // This is valid
};

This does not work when running the app in Electron 5.5.1

Hello,

I have the some problem, but only when my angular project is build for production ... what could be the issue ?

I have the same problem after I have done ng build --prod. Something happens during the conversion from 'ts' to 'js'...

```
const fileReader = new FileReader();
fileReader.onload = (event: Event) => {
this.result; // This works
};

This is how I solved it, in production:

let FileReader: new() => FileReader = ((window as any).FileReader as any).__zone_symbol__OriginalDelegate
let reader = new FileReader();

Based on this StackOverflow post:

https://stackoverflow.com/questions/45542462/angular-4-x-cordova-filereader-fails-silently-white-screen-of-death/45542463?noredirect=1#comment88157606_45542463

How about this:


export interface IDBEventTarget extends EventTarget {
  result: IDBDatabase;
}
export interface IDBEvent extends IDBVersionChangeEvent {
  target: IDBEventTarget;
}

...
request.onupgradeneeded = ( e: IDBEvent ) => {
      this.db = e.target.result;
});

const fileReader = new FileReader();
fileReader.onload = (event: Event) => {
      this.result; // This works
};

using this with arrow function will not give the expected result.

const fileReader = new FileReader();
fileReader.onload = function(event: Event) => {
      this.result;              // This works
      fileReader.result;    // Even this works - by @LouisWayne  
};
Was this page helpful?
0 / 5 - 0 ratings