如何解决此“主题错误地扩展了可观察对象"的问题, 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?

查看:157
本文介绍了如何解决此“主题错误地扩展了可观察对象"的问题, TypeScript 2.4和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

如果没有,下面的解决方案应该起作用.

If not, the below solution should work.

TypeScript 2.4更改了严格性,并且Subject<T>没有提升到正确的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.

这篇关于如何解决此“主题错误地扩展了可观察对象"的问题, TypeScript 2.4和RxJS 5.x中出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆