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

前端 未结 14 1699
情书的邮戳
情书的邮戳 2020-12-23 14:48

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 re

相关标签:
14条回答
  • 2020-12-23 15:09

    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
    };
    
    0 讨论(0)
  • 2020-12-23 15:12

    If anyone is finding the simplest solution then this worked for me.

    var reader:any,
    target:EventTarget;
    reader= new FileReader();
    reader.onload = function (imgsrc){
    //var fileUrl = imgsrc.target.result; //change to
    var fileUrl = (imgsrc.target as FileReader).result; //cast to correct type
    }
    
    0 讨论(0)
  • 2020-12-23 15:13

    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 {
    
    0 讨论(0)
  • 2020-12-23 15:13

    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;
    }
    
    0 讨论(0)
  • 2020-12-23 15:13

    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

    0 讨论(0)
  • 2020-12-23 15:13

    The target object can be accessed as below to prevent error until fix:

    reader= new FileReader();
    reader.onload = function (imgsrc){
        var fileUrl = imgsrc.target["result"];
    }
    

    Treating the target as a Javascript Object

    0 讨论(0)
提交回复
热议问题