我已经为JavaScript Array创建了一个polyfill;如何通知TypeScript编译器扩展到JS Array原型?
if (Array.prototype.remove !== 'function') {
Array.prototype.remove = function (value) {
var idx = this.indexOf(value);
if (idx !== -1) {
return this.splice(idx, 1);
}
return false;
};
}
现在我更新原有的JavaScript项目的打字稿项目和TSC埋怨卸下摆臂方法的用法:
class Archive {
documents: DocInfo[] = []; // <-- Array of class DocInfo
addDocument(document: DocInfo) {
...
}
deleteDocument(document: DocInfo) {
this.documents.remove(document);
^^^^^^
tsc complains here: TS2339:Property 'remove' does not exist on type 'DocInfo[]'
}
}
我怎样才能知道这个扩展的TSC?
我试图创建分型文件,但没有成功:
declare module 'Array' {
export function removeByAttr(propertyName: string, propertyValue: any);
}
感谢
为什么不使它成为'DocType'本身的一部分。 – Rajesh
remove方法需要成为DocType的“数组”的一部分。不在DocType本身! – mvermand
将它移动到Array.prototype将使它可用于任何类型的数组。 – Rajesh