新的Typescript 1.8.4构建错误:“生成:属性'result'在'EventTarget'类型上不存在. " [英] New Typescript 1.8.4 build error: " Build: Property 'result' does not exist on type 'EventTarget'. "
问题描述
我是打字稿新手.在我的Durandal应用程序中,我将VS-2012迁移到VS-2015,这意味着将打字稿0.9转换为打字稿1.8.4.迁移后,我遇到了很多构建错误.我解决了除一个以外的所有问题.我遇到以下有关事件类型的构建错误.
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.
错误:内部版本:类型'EventTarget'不存在属性'result'"
代码与下面的代码完全一样:
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"正在接受类型事件.
"Imgsrc" is taking type event.
它与打字稿0.9可以正常工作,但对于1.8.4来说,它会抛出错误,因为'EventTarget'类型上不存在'结果'. 谁能帮忙解决这个问题.
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.
注意:"target:EventTarget"是从lib.d.ts获取的
推荐答案
any
是药物(几乎可以用于任何用途,但是... TypeScript的优点在哪里) ...报告了类似的问题,并提出了不错的(TypesScript-ish)解决方法
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
让我引用:
我遇到了这个TS2339:类型上的属性结果"不存在 加载JS FileReader时出现"EventTarget",并针对 传递给FileReader的onerror的事件的getSummary().
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;
}
然后在我的应用中:
reader.onload = function(fre:FileReaderEvent) {
var data = JSON.parse(fre.target.result);
...
}
并且,直到lib.d.ts进行了一些更改,我们仍然可以使用已知接口进行工作
And, until some change in lib.d.ts, we still do work with known interface
2019年12月修改:
EDIT Dec 2019:
使用此修复程序,您可能会得到
With this fix, you might be getting
错误TS2322:类型'(this:FileReader,e:FileReaderEvent)=> void'不能分配给类型'(this:FileReader,ev:ProgressEvent)=>任何'.
error TS2322: Type '(this: FileReader, e: FileReaderEvent) => void' is not assignable to type '(this: FileReader, ev: ProgressEvent) => any'.
如果是这样,只需替换
interface FileReaderEvent extends Event {
使用
interface FileReaderEvent extends ProgressEvent {
这篇关于新的Typescript 1.8.4构建错误:“生成:属性'result'在'EventTarget'类型上不存在. "的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!