我在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.json 和 package.json 与它们相同,只是有更多的包。
如何摆脱这个错误?
答案 0 :(得分:4)
内置库dom
声明导致定义的问题(请参阅lib.dom.d.ts):
getUserMedia(constraints: MediaStreamConstraints): PromiseLike<MediaStream>;
而@types/webrtc
声明了您的预期定义(请参阅MediaStream.d.ts):
getUserMedia(constraints: MediaStreamConstraints): Promise<MediaStream>;
TypeScript存储库中有open issue。