Javascript Can';t在Typescript中重写/扩展类型
对于Javascript Can';t在Typescript中重写/扩展类型,javascript,intellij-idea,typescript,Javascript,Intellij Idea,Typescript,对于FileList和DOMStringList数组上的数组函数,我遇到了ts编译器错误的问题 (83,34):错误TS2339:类型“FileList”上不存在属性“forEach”。 我试图通过如下扩展类型来覆盖lib.d.ts中的默认类型: interface FileList { length: number; item(index: number): File; [index: number]: File; filter(callback: Function, this
FileList
和DOMStringList
数组上的数组函数,我遇到了ts编译器错误的问题
(83,34):错误TS2339:类型“FileList”上不存在属性“forEach”。
我试图通过如下扩展类型来覆盖lib.d.ts
中的默认类型:
interface FileList {
length: number;
item(index: number): File;
[index: number]: File;
filter(callback: Function, thisArg?: any);
find(callback: Function, thisArg?: any);
findIndex(callback: Function, thisArg?: any);
forEach(callback: Function);
indexOf(searchElement: any, fromIndex?: any);
push(elements: any);
sort(compareFunction: Function);
}
declare var FileList: {
prototype: FileList;
new(): FileList;
};
interface DOMStringList {
length: number;
contains(str: string): boolean;
item(index: number): string;
[index: number]: string;
filter(callback: Function, thisArg?: any);
find(callback: Function, thisArg?: any);
findIndex(callback: Function, thisArg?: any);
forEach(callback: Function);
indexOf(searchElement: any, fromIndex?: any);
push(elements: any);
sort(compareFunction: Function);
}
declare var DOMStringList: {
prototype: DOMStringList;
new(): DOMStringList;
}
IntelliJ正确链接到myts fixes.d.ts
文件,并且不会将.forEach
突出显示为缺少的属性
但是,编译器仍然抱怨myFileList
Type上缺少属性forEach
为什么编译器不使用我的扩展接口?这与在默认库中声明类型的方式相同:如果我将扩展接口放在同一个文件中,则是这样。但如果我想在一个单独的文件中全局声明它,就不需要了