我如何解决这个“主题错误地扩展了 Observable"?TypeScript 2.4 和 RxJS 5.x 中的错误? [英] How do I get around this "Subject incorrectly extends Observable" error in TypeScript 2.4 and RxJS 5.x?
问题描述
当我编译时,我在 RxJS 声明文件中收到以下编译器错误:
When I compile, I get the following compiler error in the RxJS declaration files:
node_modules/rxjs/Subject.d.ts(16,22): error TS2415: Class 'Subject<T>' incorrectly extends base class 'Observable<T>'.
Types of property 'lift' are incompatible.
Type '<R>(operator: Operator<T, R>) => Observable<T>' is not assignable to type '<R>(operator: Operator<T, R>) => Observable<R>'.
Type 'Observable<T>' is not assignable to type 'Observable<R>'.
Type 'T' is not assignable to type 'R'.
这里发生了什么,我如何在不降级到 TypeScript 2.3 或更早版本的情况下解决这个问题?
What's going on here, and how do I get around this without downgrading to TypeScript 2.3 or earlier?
推荐答案
解决方案
RxJS 5.4.2 现在应该可以与 TypeScript 2.4.1 完美配合.如果可能,只需升级到 5.4.2+.
npm install --save rxjs@^5.4.2
如果没有立即看到更改,请尝试重新启动编辑器和/或重新编译.
Then try restarting your editor and/or recompile if you don't see an immediate change.
如果没有,下面的解决方案应该可以工作.
If not, the below solution should work.
TypeScript 2.4 有严格的变化,Subject
没有提升到正确的 Observable
.签名真的应该是
TypeScript 2.4 has a strictness change, and Subject<T>
isn't lifting to the correct Observable
. The signature really should have been
<R>(operator: Operator<T, R>) => Observable<R>
这将在 RxJS 6 中修复.
This will be fixed in RxJS 6.
较新版本的 RxJS 将修复此问题,但作为临时解决方法,您可以使用 noStrictGenericChecks
编译器选项.
Newer versions of RxJS will have this fixed, but as a temporary workaround, you can use the noStrictGenericChecks
compiler option.
在tsconfig.json
中,将其放入"compilerOptions"
并设置为true
.
In tsconfig.json
, put it in "compilerOptions"
and set it to true
.
{
"compilerOptions": {
"noStrictGenericChecks": true
}
}
在命令行上是 --noStrictGenericChecks
.
这篇关于我如何解决这个“主题错误地扩展了 Observable"?TypeScript 2.4 和 RxJS 5.x 中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!