扩展lib.d.ts接口?

时间:2016-06-18 07:22:59

标签: intellij-idea typescript

我有一些处理drop事件的代码,并检查我是否必须处理丢弃的文件:

public handleDrop(evt: DropEvent): boolean {
  var isUpload: boolean; = false;
  var dt: DataTransfer = evt.DataTransfer;

  dt.types.forEach((type)=>{
    // dt.types is of type DOMStringList
    if(type === "File"){
      isUpload = true;
    }
  })
  return isUpload;
}

它的工作正常,但TS编译器给出了以下错误消息:

(117,16): error TS2339: Property 'forEach' does not exist on type 'DOMStringList'.

首先,DataTransfer.types将始终是一个数组,因此根据我的理解,我应该可以访问它的数组方法。

我尝试通过将自定义DOMStringList Interface合并到我lib.d.ts中定义的自定义declare interface DOMStringList { forEach(cb: Function): any; } 来为我的问题创建解决方法:

dt.types.forEach

IntelliJ识别界面,不再显示DOMStringList的错误。

但是我的ts编译器仍然会抛出错误,似乎无法识别我的合并接口。

告诉我的ts编译器我应该能够在FileList上使用数组函数的正确方法是什么? 其他泛型类型也会出现此问题,例如ts.config.ts

编辑: { "compilerOptions": { "target": "ES5", "module": "commonjs", "noEmitOnError": false, "noImplicitAny": false, "removeComments": true, "preserveConstEnums": true, "sourceMap": true }, "files": [ "app/typings/app.typings.ts", "typings/tsd.d.ts" ] }

[
  {
    "directory": "/source",
    "command": "clang++ <file>.m",
    "file": "/source/div0.c"
  }
]

0 个答案:

没有答案