Я создал полипол для JavaScript-массива;Как проинформировать компилятор 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 жалуется на использовании в .remove методы:
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[]'
}
}
Как я могу сказать ТСК об этом расширении?
Я попытался создать файл типизации, но без какого-либо успеха:
declare module 'Array' {
export function removeByAttr(propertyName: string, propertyValue: any);
}
Благодарности
Почему бы не сделать его частью 'DocType'. – Rajesh
Метод удаления должен быть частью «Массив» DocType. Не на самом DocType! – mvermand
Перемещение его в Array.prototype сделает его доступным для любого типа массива. – Rajesh