Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
oriondean opened this issue Aug 5, 2015 · 13 comments · Fixed by microsoft/TypeScript-DOM-lib-generator#398
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@oriondean
Copy link

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 oriondean changed the title FileReader.onLoad event argument has no target.result property FileReader.onLoad/onLoadEnd event argument has no target.result property Aug 5, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Aug 5, 2015

looks related to #299.

@mhegazy mhegazy added Suggestion An idea for TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Revisit An issue worth coming back to labels Aug 5, 2015
@mhegazy mhegazy added Help Wanted You can do this and removed Revisit An issue worth coming back to labels Dec 10, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Dec 10, 2015

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

@JasonGoemaat
Copy link

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

@zhengbli
Copy link
Contributor

zhengbli commented Mar 1, 2017

Should be covered by microsoft/TypeScript-DOM-lib-generator#207

@victorprocure
Copy link

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.

@LouisWayne
Copy link

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

@BernardoMB
Copy link

This does not work when running the app in Electron 5.5.1

@anouarcharif
Copy link

Hello,

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

@mhegazy mhegazy self-assigned this Mar 30, 2018
@mhegazy mhegazy modified the milestones: Community, TypeScript 2.9 Mar 30, 2018
@mhegazy mhegazy added the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Mar 30, 2018
@mhegazy mhegazy reopened this Apr 2, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 2, 2018
@mhegazy mhegazy modified the milestones: TypeScript 2.9, TypeScript 2.8.2 Apr 2, 2018
@mhegazy mhegazy removed the Fixed in TSJS repo Fix merged in https://github.com/Microsoft/TSJS-lib-generator, but not ported yet label Apr 2, 2018
@charlesr1971
Copy link

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

@TonnTamerlan
Copy link

TonnTamerlan commented Jul 19, 2018

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

@charlesr1971
Copy link

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

@mattiLeBlanc
Copy link

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

@thesuraj
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet