2015-03-31 84 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