Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 569
Rayudu Avatar asked Mar 04 '16 06:03

Rayudu


People also ask

How do you solve property value does not exist on type EventTarget?

js error "Property 'value' does not exist on type EventTarget" occurs when the type of the event parameter is incorrect. To solve the error, type the event as React. ChangeEvent<HTMLInputElement> . You can then access the value as event.

Does not exist on type EventTarget?

The error "Property 'value' does not exist on type 'EventTarget'" occurs when we try to access the value property on an element that has a type of EventTarget . To solve the error, use a type assertion to type the element correctly before accessing the property. This is the index.

What is the type of event target in typescript?

EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.


2 Answers

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 }; 
like image 191
KimchiMan Avatar answered Sep 27 '22 21:09

KimchiMan


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 { 
like image 41
Radim Köhler Avatar answered Sep 27 '22 20:09

Radim Köhler