【发布时间】:2017-08-02 16:19:24
【问题描述】:
我遇到了 typescript 和 RxJs v5 的问题。
请查看更新部分。
我做了yarn add @reactivex/rxjs(也是yarn add rxjs),在我的index.ts 上做了import Rx from '@reactivex/rxjs'; 并得到了这个错误:
另外,如果我运行node ./node_modules/.bin/tsc,我也会收到此错误error TS2307: Cannot find module '@reactivex/rxjs'.
更新
也在做
import { Observable } from 'rxjs/Observable'
更新 2
这似乎与 TS 本身有关,而不是与 RxJS 相关。
"compilerOptions": {
"module": "commonjs",
"allowJs": true,
"outDir": "dist",
"target": "es2015",
"noImplicitAny": true,
"noEmit": true,
"strictNullChecks": true,
"suppressExcessPropertyErrors": false,
"suppressImplicitAnyIndexErrors": false,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"lib": [
"es2015",
"dom"
]
}
拥有这个 ^ 配置似乎可以解决 VSCode 问题,但针对 src/index.ts 运行 tsc 不起作用
node_modules/rxjs/Observable.d.ts(69,60): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
【问题讨论】:
-
我怀疑原因在于工具。
package.json有点不寻常,有"main": "index.js"和"typings": "./dist/cjs/Rx.d.ts"。这些工具可能没有使用typings条目。无论如何,如果您使用其他任何具有 RxJS 依赖项的东西,您应该安装npm install rxjs,因为这可能是其他依赖项可能使用的。 -
嘿,Cartant,您能详细说明一下吗?我想我没有在这里关注你。
-
@reactivex/rxjs没有与package.json中称为main的文件平行的.d.ts文件;.d.ts嵌套更深。我猜这可能会混淆一些工具。如果你有其他使用rxjs的依赖项(例如 Angular),TypeScript 可能会不高兴,因为@reactivex/rxjs中的类型将被视为不同的类型。我会推荐马丁的回答。
标签: typescript rxjs rxjs5