【问题标题】:How can I explicitly declare a variable of some Phaser type?如何显式声明某个 Phaser 类型的变量?
【发布时间】:2019-01-12 06:40:38
【问题描述】:

我正在尝试将PhaserBrunchTypeScript 一起使用sammebrunch-phaser2 骨架,但我遇到了TypeError

$ git clone https://github.com/samme/brunch-phaser2.git --branch typescript
$ cd brunch-phaser2
$ npm install
$ npm run start

> a-phaser-game@0.0.1 start /home/sam/github/samme/brunch-phaser2
> brunch watch --server

13:49:11 - info: application started on http://localhost:3333/
13:49:12 - error: Compiling of app/initialize.ts failed. TypeError: Cannot read property 'length' of undefined
Stack trace was suppressed. Run with `LOGGY_STACKS=1` to see the trace. 
13:49:12 - info: compiled 5 files into 2 files, copied 3 in 1.5 sec

当我向我的game 变量添加显式类型时,my Phaser experimention repo 中会出现一个稍微不同的TypeError

$ git clone https://github.com/samestep/phaser-nonsense.git --branch type-error
$ cd phaser-nonsense
$ npm install
$ npm run start

> @ start /home/sam/github/samestep/phaser-nonsense
> brunch watch --server

13:56:42 - info: application started on http://localhost:3333/
13:56:42 - error: Compiling of app/initialize.ts failed. Error: Error 2503: Cannot find namespace 'Phaser'. (Line: 3, Col: 13)
Stack trace was suppressed. Run with `LOGGY_STACKS=1` to see the trace. 
13:56:42 - info: compiled 2 files into vendor.js, copied index.html in 1.3 sec

但是在任何一种情况下,当我改变这个时:

const game: Phaser.Game = new Phaser.Game({});

到这里:

const game = new Phaser.Game({});

TypeError 消失了。

为什么这个显式类型声明会导致TypeError 发生,如何在不删除显式类型声明的情况下解决错误?

【问题讨论】:

    标签: typescript types typeerror phaser-framework brunch


    【解决方案1】:

    Brunch TypeScript 插件不支持正确解析引用 (documentation)。他们的建议是将plugins: { brunchTypescript: { ignoreErrors: true } } 添加到您的brunch-config.js 并单独运行tsc --noEmit -p . 以检查错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      相关资源
      最近更新 更多