我需要使用新的原型函数移动扩展本机数组,从而基本上对数组中的项目进行分类。但是我在编译
上遇到了错误Array.prototype.move = function (old_index, new_index) {
while (old_index < 0) {
old_index += this.length;
}
while (new_index < 0) {
new_index += this.length;
}
if (new_index >= this.length) {
var k = new_index - this.length;
while ((k--) + 1) {
this.push(undefined);
}
}
this.splice(new_index, 0, this.splice(old_index, 1)[0]);
//return this; // for testing purposes
};
您遇到的错误可能是:
属性'移动'不存在于类型的'home []'。
的价值上
要解决此问题,除了代码外,还需要在数组接口上定义方法。例如:
interface Array {
move(old_index: number, new_index: number): void;
}
注意:在打字稿1.0中,您必须写interface Array<T>