【问题标题】:error TS2339: Property 'catch' does not exist on type 'PromiseLike<void>'错误 TS2339:类型“PromiseLike<void>”上不存在属性“catch”
【发布时间】:2016-08-14 04:55:29
【问题描述】:

我在 Angular 2 中使用 WebRTC。

TypeScript 1.x中,我可以成功使用这个。

   navigator.mediaDevices.getUserMedia(constraints)
      .then(myStream => {
        this.myStream = myStream;
      })
      .catch(error => {
        console.log(error);
      });

但更新到 TypeScript 2.x 后,我在终端运行 npm run watch 时出现此错误:

错误 TS2339:类型上不存在属性“catch” 'PromiseLike'。

我的 IDE WebStore 也显示为红色:

我已经做了npm install --save-dev @types/webrtc

我的 tsconfig.json 文件:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true,
    "lib": ["es6", "dom"],
    "types": [
      "body-parser",
      "compression",
      "express",
      "express-session",
      "mime",
      "node",
      "serve-static",
      "webrtc",
      "ws"
    ]
  },
  "include": [
    "node_modules/@types/**/*.d.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "!node_modules/@types/**/*.d.ts"
  ],
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

我使用universal-starter作为开始,所以我的nodemon.jsonpackage.json和他们一样,只是有更多的包。

我怎样才能摆脱这个错误?

【问题讨论】:

  • getUserMedia 返回什么?
  • @torazaburo 它返回Promise&lt;MediaStream&gt;
  • @HongboMiao 请发布完整的错误消息(带有调用堆栈)。如果我运行您的示例,我将无法重现此错误。 getUserMedia.then 在我的设置中返回 Promise 而不是 PromiseLike。因此,我没有收到错误。此错误是否出现在您的“真实代码”中的不同位置,或者您是否使用此确切代码收到此错误?
  • @HongboMiao 请发布您的tsconfig.json

标签: angular typescript promise getusermedia typescript2.0


【解决方案1】:

内置库dom 声明了导致定义的问题(参见lib.dom.d.ts):

getUserMedia(constraints: MediaStreamConstraints): PromiseLike<MediaStream>;

@types/webrtc 声明了您的预期定义(参见MediaStream.d.ts):

getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream>;

TypeScript 存储库中有一个 open issue

【讨论】:

  • 哦,我高兴得太早了。更改为"lib": ["es6", "dom", "es2015.promise"], 后,当我运行npm run watch 时,终端中仍然显示相同的错误@
  • "es6" 包括"es2015.promise"。因此,您不需要"es2015.promise"
  • watch 脚本在您的package.json 中是什么样子的?
  • 我在我的问题中添加了更多细节。由于我使用的是通用启动器,所以watch 脚本就像this line。 (请注意,我的 IDE WebStorm 对于catch 也显示为红色)
  • 现在我猜这是@types/webrtc 的一个错误,因为当我在其他像猫鼬上使用promise 和catch 时,它不会给我任何错误。
猜你喜欢
  • 2018-07-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2019-04-11
  • 2019-01-21
  • 2016-08-13
相关资源
最近更新 更多