【发布时间】:2020-05-29 17:02:54
【问题描述】:
我正在尝试为 Array 创建一个扩展方法。
这是我创建的:
interface Array<T> {
moveServiceBranchInArray(): void;
}
Array.prototype.moveServiceBranchInArray = function<T> (array: T[], pred: (x: T) => boolean, index: number): void
{
const curPos: number = array.findIndex(pred);
index = Math.max(Math.min(index, array.length - 1), 0);
if (curPos < 0) {
return;
}
[array[curPos], array[index]] = [array[index], array[curPos]];
}
不幸的是,我收到了错误
"类型 '(array: T[], pred: (x: T) => boolean, index: number) => void' 不能分配给类型 '() => void'。"
如标题所示。什么给了,帮助表示赞赏。
【问题讨论】:
标签: typescript extension-methods typescript-generics