2014-01-21 38 views
2

我需要使用新的原型函數移動來擴展本地數組,該移動對數組中的項進行基本排序。但我對編譯遇到錯誤`Typescript 0.9.5使用移動功能擴展數組原型

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 
}; 

回答

7

你所得到的錯誤大概是:

屬性「搬家」不會對類型爲「任何[]」存在的價值。

要解決這個問題,除了代碼外,還需要在Array接口上定義方法。例如:

interface Array { 
    move(old_index: number, new_index: number): void; 
} 

注:在打字稿1.0你必須寫interface Array<T>

+0

謝謝回答。我收到你剛寫的錯誤。不幸的是,我嘗試使用接口Array(old_index:number,new_index:number):void; }但我仍然有同樣的問題。 – Roman

+1

@羅曼它工作正常:http://goo.gl/YoPmav – basarat

+0

在我的電腦打字稿0.9.5。我仍然有這個錯誤。我安裝了resharper 8.1和Web Essensials。 – Roman