2015-03-31 86 views
1

鑑於這種如何聲明添加到原型鏈的方法?

class GameActions { 
    bootstrap() { 
    return fetchGames().then((data: any) => { 
     // error 
     this.dispatch(data) 
    }) 
    } 
} 

module.exports = alt.createActions(GameActions) 

如何通知打字稿是dispatch在原型鏈中發現了什麼? alt.createActions通過原型繼承增加了dispatch。此外,alt是來自npm的外部通量庫。

回答

2

alt.createActions(GameActions)

這實際上是一個mixin。目前在TypeScript中沒有關於mixin的好故事。還有就是如何鍵入文檔:https://github.com/Microsoft/TypeScript/wiki/Mixins

你基本上聲明這些成員存在,但不定義他們,即:

class GameActions { 
    bootstrap() { 
    return fetchGames().then((data: any) => { 
     this.dispatch(data) 
    }) 
    } 

    // Dispatchable: 
    dispatch: Function; 
} 

module.exports = alt.createActions(GameActions) 

混入都在路線圖2.0:http://github.com/Microsoft/TypeScript/wiki/Roadmap#20你也可以在這裏開始討論:http://github.com/Microsoft/TypeScript/issues如果你能提出一個建議,這將是一件好事

+0

清除它。 Typescript需要一個'become'關鍵字,這意味着它將在未來實現一些接口。在我的例子中,'GameActions類變成ActionCreator' – mgutz 2015-04-01 05:21:15

+0

Mixin在2.0的路線圖上:https://github.com/Microsoft/TypeScript/wiki/Roadmap#20你也可以在這裏開始討論:https:// github。 com/Microsoft/TypeScript/issues如果你能提出一個建議,那將是非常好的。 – basarat 2015-04-01 06:05:03