RxJS5 TypeScript 打字失败

RxJS5 TypeScript typings fail

我 运行 tsc 在我的项目上,我得到了这些与 RxJS5 库相关的错误:

$ tsc
node_modules/rxjs/observable/FromEventObservable.d.ts(11,39): error TS2304: Cannot find name 'EventTarget'.
node_modules/rxjs/observable/FromEventObservable.d.ts(11,103): error TS2304: Cannot find name 'NodeList'.
node_modules/rxjs/observable/FromEventObservable.d.ts(11,114): error TS2304: Cannot find name 'HTMLCollection'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(16,23): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(77,59): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(82,13): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(97,20): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(98,10): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(108,32): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(108,44): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(119,10): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(128,39): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/AjaxObservable.d.ts(136,22): error TS2304: Cannot find name 'XMLHttpRequest'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(10,29): error TS2304: Cannot find name 'MessageEvent'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(11,33): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(12,34): error TS2304: Cannot find name 'CloseEvent'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(15,63): error TS2304: Cannot find name 'WebSocket'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(27,13): error TS2304: Cannot find name 'WebSocket'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(28,32): error TS2304: Cannot find name 'Event'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(29,33): error TS2304: Cannot find name 'CloseEvent'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(32,63): error TS2304: Cannot find name 'WebSocket'.
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts(36,23): error TS2304: Cannot find name 'MessageEvent'.

有谁知道为什么会这样?这是我的 package.json 文件:

{
  "name": "oplog.rx",
  "version": "0.0.101",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "suman test"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ORESoftware/oplog.rx.git"
  },
  "keywords": [
    "oplog",
    "mongodb",
    "rx",
    "rxjs"
  ],
  "author": "Olegzandr V.D.",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/ORESoftware/oplog.rx/issues"
  },
  "homepage": "https://github.com/ORESoftware/oplog.rx#readme",
  "dependencies": {
    "bson": "^2.0.4",
    "json-stdio": "0.0.1016",
    "mongodb": "^3.0.4",
    "rxjs": "^5.5.7"
  },
  "devDependencies": {
    "@types/mongodb": "^3.0.8"
  }
}

您需要将 "dom" 添加到 tsconfig.json 文件的 compilerOptions:

中的 lib 设置
"lib": ["dom", "es2015"]

或者,您可以使用 skipLibCheck compilerOption 来避免检查 node_modules 中的 .d.ts 个文件:

"skipLibCheck": true

有关解释,请参阅 this issue

基本上,fromEvent 可观察工厂同时支持 Node 和 DOM 事件源,TypeScript 需要了解 DOM 类型。

This PR 应该可以解决问题。