如何声明添加到原型链的方法?

How to declare methods added to the prototype chain?

鉴于此

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

module.exports = alt.createActions(GameActions)

如何通知 typescript 在原型链中找到 dispatchalt.createActions 通过原型继承添加 dispatch。此外,alt 是来自 npm 的外部助焊剂库。

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)

Mixins 在 2.0 的路线图上:http://github.com/Microsoft/TypeScript/wiki/Roadmap#20 Also you can start a discussion here : http://github.com/Microsoft/TypeScript/issues如果你能提出一个建议就太好了