TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open oriondean opened this issue 11 years ago • 13 comments

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)

oriondean avatar Aug 05 '15 14:08 oriondean

looks related to #299.

mhegazy avatar Aug 05 '15 21:08 mhegazy

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

mhegazy avatar Dec 10 '15 03:12 mhegazy

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

JasonGoemaat avatar Feb 20 '17 09:02 JasonGoemaat

Should be covered by https://github.com/Microsoft/TSJS-lib-generator/pull/207

zhengbli avatar Mar 01 '17 20:03 zhengbli

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.

victorprocure avatar Aug 12 '17 00:08 victorprocure

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
};

LouisWayne avatar Sep 24 '17 00:09 LouisWayne

This does not work when running the app in Electron 5.5.1

BernardoMB avatar Dec 26 '17 04:12 BernardoMB

Hello,

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

anouarcharif avatar Jan 05 '18 17:01 anouarcharif

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

charlesr1971 avatar May 27 '18 20:05 charlesr1971

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

TonnTamerlan avatar Jul 19 '18 20:07 TonnTamerlan

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

charlesr1971 avatar Jul 19 '18 23:07 charlesr1971

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;
});

mattiLeBlanc avatar Aug 24 '18 06:08 mattiLeBlanc

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  
};

thesuraj avatar Dec 31 '19 11:12 thesuraj