错误 TS2304:找不到名称 'RTCPeerConnection'

error TS2304: Cannot find name 'RTCPeerConnection'

我在 Angular 2.

中使用 WebRTC

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

const peerConnection = new RTCPeerConnection(configuration, null);

但是在更新到 TypeScript 2.x 之后,我的终端出现了这个错误:

error TS2304: Cannot find name 'RTCPeerConnection'.

我已经 npm install --save-dev @types/webrtc,并且我的 IDE WebStorm 已经 link 正确输入了 RTCPeerConnection

RTCPeerConnection 的输入在 /my-project/node_modules/@types/webrtc/RTCPeerConnection.d.ts

我的tsconfig.json文件:

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "module": "commonjs",
    "removeComments": true,
    "sourceMap": true,
    "lib": ["es6", "dom"]
  },
  "include": [
    "node_modules/@types/**/*.d.ts",
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "!node_modules/@types/**/*.d.ts"
  ],
  "compileOnSave": false,
  "buildOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

如何正确操作?

@types/webrtc 是全局类型定义。添加

"types": [
  "webrtc"
]

给你的compilerOptions。提到 types 选项 here.