New Typescript 1.8.4 build error: " Build: Property 'result' does not exist on type 'EventTarget'. "

51,129

Solution 1

While any is a medicine (almost for anything, but... where is the TypeScript benefit then)... there is a similar issue reported and nice (TypesScript-ish) workaround suggested

Request to change currentTarget in Event interface for lib.d.ts

let me cite:

I ran into this TS2339: Property 'result' does not exist on type 'EventTarget' in JS FileReader onload, and another warning for getSummary() on the event passed to FileReader's onerror.

My work-around, to suppress the horrid red squiggily lines;-) is the following:

interface FileReaderEventTarget extends EventTarget {
    result:string
}

interface FileReaderEvent extends Event {
    target: FileReaderEventTarget;
    getMessage():string;
}

Then in my app:

reader.onload = function(fre:FileReaderEvent) {
    var data = JSON.parse(fre.target.result);
    ...
}

And, until some change in lib.d.ts, we still do work with known interface

EDIT Dec 2019:

With this fix, you might be getting

error TS2322: Type '(this: FileReader, e: FileReaderEvent) => void' is not assignable to type '(this: FileReader, ev: ProgressEvent) => any'.

If so, just replace

 interface FileReaderEvent extends Event {

with

 interface FileReaderEvent extends ProgressEvent {

Solution 2

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

Solution 3

With my old type script the parameter "imgsrc" is having any type by default.

So, now I made it as (imgsrc:any). It's working fine.

var reader:any,
target:EventTarget;
reader= new FileReader();
reader.onload = function (imgsrc:any){
var fileUrl = imgsrc.target.result;
}

Solution 4

The issue is with the typescript definitions. A simple cheat is:

let target: any = e.target; //<-- This (any) will tell compiler to shut up!
let content: string = target.result;

Solution 5

Just let TypScript know what type you would expect it to be.

Here is the fix:

let reader = new FileReader();
reader.onload = function (event){
    let fileUrl = (<FileReader>event.target).result;
}

You could also use reader.result instead in this case

Share:
51,129

Related videos on Youtube

Rayudu
Author by

Rayudu

working as a UI developer ( HTML, CSS, Javascript)

Updated on June 03, 2021

Comments

  • Rayudu
    Rayudu about 3 years

    I am New to typescript. In my Durandal application I migrated to VS-2012 to VS-2015 means typescript 0.9 to typescript 1.8.4. After migrated I got so many build errors. I resolved all those except one. I am getting below build error on types of Events.

    ERROR: " Build: Property 'result' does not exist on type 'EventTarget' "

    And the code was exactly like this below:

    var reader:any,
    target:EventTarget;
    
    reader= new FileReader();
    reader.onload = function (imgsrc){
        var fileUrl = imgsrc.target.result;
    }
    

    "Imgsrc" is taking type event.

    It's working fine with typescript 0.9 but with 1.8.4 it's throwing error as 'result' does not exist on type 'EventTarget'. Can any one help on this to resolve.

    Note: "target:EventTarget" is getting from lib.d.ts

  • Revils
    Revils over 7 years
    Nice solution, however when I restarted the solution it gives many errors in the lib.d.ts. It collides with the real EventTarget interface.
  • Fergal Moran
    Fergal Moran about 6 years
    Still unfixed as of late April 2018
  • b00r00x0
    b00r00x0 about 6 years
    Still unfixed as of early June 2018
  • Sam
    Sam almost 6 years
    Still unfixed as of late July 2018
  • Azoulay Jason
    Azoulay Jason almost 6 years
    Still unfixed as of late August 2018
  • Jeff Huijsmans
    Jeff Huijsmans almost 6 years
    Still unfixed. October 2018.
  • Dawit
    Dawit over 5 years
    still unfixed. Nov 4,2018
  • roadev
    roadev over 5 years
    can't parse as a json :(
  • Victor Fernandes
    Victor Fernandes over 5 years
    Still unfixed as of Jan 2019
  • Stack Underflow
    Stack Underflow over 5 years
    I needed to use ArrayLike<number> instead of string in the FileReaderEventTarget interface.
  • Dila Gurung
    Dila Gurung over 5 years
    @KimchiMan.. Great ... Can you explain how.?
  • TSR
    TSR over 5 years
    Still unfixed as of March 2019
  • Stephan Schinkel
    Stephan Schinkel about 5 years
    Still unfixed as of May 2019
  • Tonio
    Tonio almost 5 years
    Still unfixed. July 2019
  • Adam Schneider
    Adam Schneider almost 5 years
    This is the best solution
  • x7R5fQ
    x7R5fQ almost 5 years
    Still unfixed. Sept 2019
  • Maravarman Manoharan
    Maravarman Manoharan almost 5 years
    If this error comes<br> Type 'string | ArrayBuffer' is not assignable to type 'string'. Type 'ArrayBuffer' is not assignable to type 'string' <br/> > fileReader.result;+'';
  • Kobe
    Kobe over 4 years
    Still unfixed. Oct 2019
  • Bogdan Ionitza
    Bogdan Ionitza over 4 years
    this should be the accepted answer, clean and no hacks involved
  • Akhil
    Akhil over 4 years
    Still unfixed as of Jan 2019
  • Melroy van den Berg
    Melroy van den Berg over 4 years
    Still not fixed as of 19 Nov 2019 :(
  • Melroy van den Berg
    Melroy van den Berg over 4 years
    Actually its ProgressEvent<FileReader> and not just Event...
  • Vincent
    Vincent over 4 years
    No need to create your own interface. You can use FileReader (which extends EventTarget) from lib.dom.ts to cast the event
  • Salketer
    Salketer over 4 years
    Added a slight tweak that may be needed for some (just like for me)
  • Benny Bottema
    Benny Bottema over 4 years
    Still unfixed as of Jan 2020
  • Pablo Servin
    Pablo Servin about 4 years
    Still unfixed as of April 2020
  • AnthonyOSX
    AnthonyOSX about 4 years
    Still unfixed as of May 2020
  • DHRUV GAJWA
    DHRUV GAJWA about 4 years
    Still unfixed as of June 2020
  • ysf
    ysf about 4 years
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation.
  • John R.
    John R. almost 4 years
    Still unfixed as of July 2020
  • user8380672
    user8380672 almost 4 years
    Still unfixed as of August 2020
  • Krzysztof Podmokły
    Krzysztof Podmokły over 3 years
    Still unfixed as of Jan 2021
  • Pallavi
    Pallavi over 3 years
    And if you get this error - 'Type 'string | ArrayBuffer' is not assignable to type 'string' ' you can simply typecast as provided here stackoverflow.com/a/54089823/13596406
  • Toxy
    Toxy almost 3 years
    Still unfixed as of Sep 2021
  • denifer santiago fernandez
    denifer santiago fernandez over 2 years
    Still unfixed as of Jan 2022