2017-04-08 20 views
0

升級到使用最新Typescript版本(2.2.1)的Ionic3後,我面臨着大問題。我有一個正常的Typescript類BoxList,它擴展了Array。所以這是正常的數組,其中還有一些我使用的自定義方法。我有一個名爲「allHit」的方法,它遍歷數組並返回布爾值。問題是,在Ionic2一切工作正常,但在升級之後,我不能再調用this.boxList.allHit方法,因爲它引發了我的異常:Typescript對象擴展數組<BoxModel>不能再調用內部方法

-> main.js:1 ERROR TypeError: this.boxList.allHit is not a function(…)

代碼:

import {BoxModel} from "./BoxModel"; 
export class BoxList extends Array<BoxModel> { 

    constructor() { 
     super(); 
    } 

    public allHit(boxTarget: BoxModel) : boolean { 
     return this.findIndex(box => box.doesMatch(boxTarget) && !box.isHit) === -1; 
    } 

    public findUntouchedBox() : BoxModel { 
     return this.find(box => !box.isHit); 
    } 
} 

而從其他對象調用allHit方法:

public allBoxesAreHit() : boolean { 
    return this.boxList.allHit(this.targetBox); 
} 

有人知道這裏發生了什麼嗎?謝謝!

+0

你可以添加功能到你的問題? –

+0

嘿!我添加了全班。 – user2364292

+0

你可以添加函數調用嗎? –

回答

0

根據打字稿2.1 breaking changes

As part of substituting the value of this with the value returned by a super(...) call, subclassing Error, Array, and others may no longer work as expected. This is due to the fact that constructor functions for Error, Array, and the like use ECMAScript 6's new.target to adjust the prototype chain; however, there is no way to ensure a value for new.target when invoking a constructor in ECMAScript 5.

嘗試他們的推薦和設置:

constructor() { 
    super(); 
    Object.setPrototypeOf(this, BoxList.prototype); 
} 
在構造

+1

哇,實際上工作!我的構造函數看起來像這樣: constructor(){ super(); Object.setPrototypeOf(this,BoxList.prototype); } – user2364292

+0

Typescript已經從2.0.9改變了很多到2.2.1 ..它相當一跳......你應該通過它的其餘部分 –

+0

我會看看更新日誌的權利!泰 – user2364292