如何解决 TypeScript 2.4 和 RxJS 5.x 中的“主题错误地扩展 Observable”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44810195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get around this "Subject incorrectly extends Observable" error in TypeScript 2.4 and RxJS 5.x?
提问by Daniel Rosenwasser
When I compile, I get the following compiler error in the RxJS declaration files:
编译时,我在 RxJS 声明文件中收到以下编译器错误:
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'.
What's going on here, and how do I get around this without downgrading to TypeScript 2.3 or earlier?
这里发生了什么,我如何在不降级到 TypeScript 2.3 或更早版本的情况下解决这个问题?
回答by Daniel Rosenwasser
Solution
解决方案
RxJS 5.4.2 should now work perfectly with TypeScript 2.4.1. Simply upgrade to 5.4.2+ if possible.
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.
如果没有,下面的解决方案应该有效。
Why it's happening
为什么会发生
TypeScript 2.4 has a strictness change, and Subject<T>
isn't lifting to the correct Observable
. The signature really should have been
TypeScript 2.4 有严格的变化,并Subject<T>
没有提升到正确的Observable
. 签名真的应该是
<R>(operator: Operator<T, R>) => Observable<R>
This will be fixed in RxJS 6.
这将在 RxJS 6 中修复。
Alternative Solution
替代方案
Newer versions of RxJS will have this fixed, but as a temporary workaround, you can use the noStrictGenericChecks
compiler option.
较新版本的 RxJS 将修复此问题,但作为临时解决方法,您可以使用noStrictGenericChecks
编译器选项。
In tsconfig.json
, put it in "compilerOptions"
and set it to true
.
在 中tsconfig.json
,将其放入"compilerOptions"
并设置为true
。
{
"compilerOptions": {
"noStrictGenericChecks": true
}
}
On the command line it's --noStrictGenericChecks
.
在命令行上是--noStrictGenericChecks
.
回答by Karthik Elumalai
Today, faced this error and solved this pratically with the below three steps. So sharing the same with the hope, it will help others.
今天,面对这个错误并通过以下三个步骤实际解决了这个问题。因此,与希望共享相同的内容,这将有助于其他人。
Step 1:In the package.json file change the entry as "rxjs": "5.4.2",
第 1 步:在 package.json 文件中将条目更改为“rxjs”:“5.4.2”,
Step 2:Delete the node_modules folder from the project,which is present in the root directory
步骤 2:从项目中删除 node_modules 文件夹,该文件夹位于根目录中
Step 3 :Now ,Right click the package.json file and click restore like as shown below:
第 3 步:现在,右键单击 package.json 文件并单击恢复,如下所示:
Note :It will again create node_module folder with the new files, Now build the solution, hopefully you should not get any build error related to above problem.
注意:它将再次使用新文件创建 node_module 文件夹,现在构建解决方案,希望您不会遇到与上述问题相关的任何构建错误。
回答by shaishav shukla
Add
添加
"noStrictGenericChecks": true
in tsconfig.json file
在 tsconfig.json 文件中
回答by Krishna
I have made the below two changes in package.json file.
我在 package.json 文件中进行了以下两项更改。
1)changed the rxjs version to 5.4.1 in dependencies section.
1)在依赖项部分将 rxjs 版本更改为 5.4.1。
"dependencies":
{
"rxjs": "5.4.1"
}
“依赖关系”:{
“rxjs”:“5.4.1”}
2)changed the typescript version to 2.4.0 in devDependencies section.
2)在 devDependencies 部分将打字稿版本更改为 2.4.0。
"devDependencies": { "typescript": "2.4.0" }
“devDependencies”:{“打字稿”:“2.4.0”}
I ran 'npm install' command after making the two changes and it worked.
在进行了两项更改后,我运行了“ npm install”命令并且它起作用了。
回答by JustLearning
In your Subject class:
在您的主题课程中:
change this line:
改变这一行:
lift<R>(operator: Operator<T, R>): Observable<T>;
to :
到 :
lift<R>(operator: Operator<T, R>): Observable<R>;
回答by Sumant Singh
I have made the below changes in package.json file.
我在 package.json 文件中进行了以下更改。
"dependencies": {
"@angular/animations": "4.1.3",
"@angular/common": "4.4.6",
"@angular/compiler": "4.4.6",
"@angular/compiler-cli": "2.4.8",
"@angular/core": "4.4.6",
"@angular/forms": "4.4.6",
"@angular/http": "4.4.6",
"@angular/platform-browser": "4.4.6",
"@angular/platform-browser-dynamic": "4.4.6",
"@angular/platform-server": "4.4.6",
"@angular/router": "4.4.6",
"@ionic/storage": "2.0.0",
"cordova-plugin-console": "1.0.5",
"cordova-plugin-device": "1.1.4",
"cordova-plugin-splashscreen": "~4.0.1",
"cordova-plugin-statusbar": "2.2.1",
"cordova-plugin-whitelist": "1.3.1",
"ionic-angular": "2.2.0",
"ionic-native": "2.4.1",
"ionic-plugin-keyboard": "~2.2.1",
"ionic-tooltips": "^2.0.0",
"ionicons": "3.0.0",
"rxjs": "^5.4.2",
"sw-toolbox": "3.4.0",
"typescript": "2.4.0",
"zone.js": "0.7.2"
}
Changes in rxjs and typescript version.
rxjs 和打字稿版本的变化。
回答by Yue Yin
This is a mismatch issue between rxjs and typscript, using "rxjs": "5.4.2" and "typescript": "~2.3.4" works for me
这是 rxjs 和 typscript 之间的不匹配问题,使用 "rxjs": "5.4.2" 和 "typescript": "~2.3.4" 对我有用
回答by Paul Thompson
I ran into the same problem and found the following website helpful https://www.georgephillipson.com/blog/jquery/setting-up-angular-2-in-visual-studio-2017/it had step by step instructions on setting up Angular for an MVC website explained how to fix the error that this post is referring to and also a link to GitHub with the full code.
我遇到了同样的问题,发现以下网站很有帮助https://www.georgephillipson.com/blog/jquery/setting-up-angular-2-in-visual-studio-2017/它有关于设置的分步说明up Angular for an MVC website 解释了如何修复这篇文章所指的错误,以及一个包含完整代码的 GitHub 链接。
I found it helpful so thought I would let others know
我发现它很有帮助所以我想我会让其他人知道
As per Zoes post, I have added more to why I think the link may be useful
根据 Zoes 的帖子,我添加了更多关于为什么我认为链接可能有用的内容
The page shows how to update the RxJS to fix error, it also shows how to check if you have Node and NPM installed on your computer and explains how to add TypeScript, it then shows how to set up your cshtml file to display Angular content
该页面展示了如何更新 RxJS 以修复错误,还展示了如何检查您的计算机上是否安装了 Node 和 NPM,并解释了如何添加 TypeScript,然后展示了如何设置您的 cshtml 文件以显示 Angular 内容
回答by Suraj
In tsconfig.json, put it in "compilerOptions" and set it to true.
在 tsconfig.json 中,将其放入“compilerOptions”并将其设置为 true。
{
"compilerOptions": true,
"compilerOptions": {
"noStrictGenericChecks": true
}
}