rxjs/Subject.d.ts错误:类'Subject <T>'错误地扩展了基类'Observable <T>'

Dee*_*k S 89 rxjs typescript angular

我从本教程中提取了示例模板代码,并按以下两个步骤开始 -

  1. npm install // worked fine and created node_modules folder with all dependencies
  2. npm start //因以下错误而失败 -

    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 '<T, 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'.
      npm ERR! code ELIFECYCLE
      npm ERR! errno 2
    
    Run Code Online (Sandbox Code Playgroud)

我在主题中看到.d.ts电梯声明如下 -

 lift<T, R>(operator: Operator<T, R>): Observable<T>;
Run Code Online (Sandbox Code Playgroud)

在Observable.ts中,定义如下 -

 lift<R>(operator: Operator<T, R>): Observable<R> {
Run Code Online (Sandbox Code Playgroud)

注意: - 1.我是Angular2的新手并试图掌握一些东西.

  1. 该错误可能是由于升力方法的定义不兼容

  2. 我通读了这个github线程

  3. 如果我需要安装一些不同版本的rxjs,请告诉我们如何卸载并安装正确的rxjs.

Edit1: 我在这里回复可能有点迟,但即使在使用typescript 2.3.4rxjs 6 alpha之后我仍然会得到相同的错误.下面是我的package.json,

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "@angular/upgrade": "2.0.0",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "6.0.0-alpha.0",
    "systemjs": "0.19.27",
    "zone.js": "^0.6.23",
    "angular2-in-memory-web-api": "0.0.20",
    "bootstrap": "^3.3.6"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.2",
    "typescript": "2.3.4",
    "typings": "^1.3.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

Cha*_*oot 70

按照这个,你需要更新打字稿2.3.4或rxjs 6个字母

在项目更新typescript或rxjs版本中转到package.json文件.例

"typescript": "2.3.4"
Run Code Online (Sandbox Code Playgroud)

npm install

更新(2017年6月29日): -

根据评论打字稿的"2.4.0"工作原理.

  • `"typescript":"^ 2.3.4"`将匹配2.4.1和2.4是暴露RxJS问题的版本. (6认同)
  • 将打字稿版本替换为"2.4.0"对我有用. (5认同)

use*_*010 24

允许使用的创可贴方法是将以下内容添加到tsconfig.json文件中,直到RxJS人员决定在使用TypeScript 2.4.1时他们想要对错误做什么:

"compilerOptions": {

    "skipLibCheck": true,

 }
Run Code Online (Sandbox Code Playgroud)

  • 真正的解决方案不应该是`skipLibCheck`.如果你这样做,你可能会对其他问题上瘾.在RxJS更新之前,更好的解决方案是`noStrictGenericChecks`. (6认同)
  • 我认为禁用或禁止所有LibCheck不是一个好主意. (2认同)

Kos*_*lun 24

正如其他人指出的那样,这个问题是由于TypeScript 2.4中引入的泛型的更严格的类型检查而产生的.这暴露了RxJS类型定义中的不一致性,因此在RxJS版本5.4.2中得到修复.所以理想的解决方案是升级到5.4.2.

如果由于某种原因无法升级到5.4.2,您应该使用Alan Haddad的扩充类型声明的解决方案来为您自己的项目修复它.即将此代码段添加到您的应用中:

// TODO: Remove this when RxJS releases a stable version with a correct declaration of `Subject`.
import { Operator } from 'rxjs/Operator';
import { Observable } from 'rxjs/Observable';

declare module 'rxjs/Subject' {
  interface Subject<T> {
    lift<R>(operator: Operator<T, R>): Observable<R>;
  }
}
Run Code Online (Sandbox Code Playgroud)

他的解决方案不会留下任何其他副作用,因此很棒.或者,建议的解决方案对您的项目设置有更多的副作用,因此不太理想:

  • 用编译器标志关闭更严格的通用检查--noStrictGenericChecks.但是,这会使您对自己的应用程序不那么严格,但是可能会引入不一致的类型定义,就像在此RxJS实例中所做的那样,这反过来可能会在您的应用中引入更多错误.
  • 不检查库中的类型 - 标志--skipLibCheck设置为true.这也不理想,因为您可能没有报告某些类型错误.
  • 升级到RxJS 6 Alpha - 鉴于存在重大变化,这可能会以严重记录的方式破坏您的应用程序,因为它仍处于alpha状态.此外,如果你有其他依赖项,如Angular 2+,这可能不是一个真正支持的选项,现在或在线破坏框架本身.我认为这是一个更难解决的问题.


Alu*_*dad 19

您可以使用" 模块扩充"临时解决此问题

以下代码为我解决了这个问题.一旦RxJS具有不会出现此问题的稳定版本,就应将其删除.

// augmentations.ts
import {Operator} from 'rxjs/Operator';
import {Observable} from 'rxjs/Observable';

// TODO: Remove this when a stable release of RxJS without the bug is available.
declare module 'rxjs/Subject' {
  interface Subject<T> {
    lift<R>(operator: Operator<T, R>): Observable<R>;
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然RxJS本身如此大量使用这种技术来描述自己的形状或许具有讽刺意味,但它实际上通常适用于一系列问题.

虽然肯定有限制和粗糙的边缘,但是这项技术强大的部分原因是我们可以使用它来增强现有的声明,使它们更安全,而不需要分叉和维护整个文件.

  • 我试过这个,因为这似乎是最不具破坏性的事情.工作得很好. (2认同)

小智 9

"dependencies": {
"@angular/animations": "^4.3.1",
"@angular/common": "^4.3.1",
"@angular/compiler": "^4.3.1",
"@angular/compiler-cli": "^4.3.1",
"@angular/core": "^4.3.1",
"@angular/forms": "^4.3.1",
"@angular/http": "^4.3.1",
"@angular/platform-browser": "^4.3.1",
"@angular/platform-browser-dynamic": "^4.3.1",
"@angular/platform-server": "^4.3.1",
"@angular/router": "^4.3.1",
"core-js": "^2.4.1",
"rxjs": "^5.4.2",
"ts-helpers": "^1.1.1",
"zone.js": "^0.7.2"
},
"devDependencies": {
"@angular/compiler-cli": "^2.3.1",
"@types/jasmine": "2.5.38",
"@types/node": "^6.0.42",
"angular-cli": "1.0.0-beta.28.3",
"codelyzer": "~2.0.0-beta.1",
"jasmine-core": "2.5.2",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "~4.0.13",
"ts-node": "1.2.1",
"tslint": "^4.3.0",
"typescript": "^2.3.4",
"typings": "^1.3.2"
}
Run Code Online (Sandbox Code Playgroud)

在package.json "rxjs": "^5.4.2","typescript":"^ 2.3.4"中,然后npm installng服务于它的工作.


Irf*_*Irf 8

只是一点点细节,试图投入我的两分钱.

当我们将Typescript升级到最新版本时出现了问题,现在它是TypeScript 2.4.1,["我们喜欢升级:) ",并且正如@ Jonnysai在他的回答中提到的那样,在那里提供了链接,关于问题及其修复的详细讨论.

那么,是什么工作对我来说是:
1.我不得不的Un安装 打字稿2.4.1首先,通过转到控制面板 > 程序和功能......
2.安装,重新,打字稿2.4.0,并确保在package.json文件,你有这个

在此输入图像描述
配置,如这里提到的更短的细节.对于Visual Studio 2015,

您可以从此处下载TypeScript 2.4.0


Dan*_*ser 5

您可以暂时使用该--noStrictGenericChecks标志来解决TypeScript 2.4中的问题.

这是--noStrictGenericChecks在命令行上,或只是"noStrictGenericChecks": true"compilerOptions"字段中tsconfig.json.

请记住,RxJS 6将更正此问题.

看到这个类似的问题:如何在TypeScript 2.4中解决RxJS 5.x中的此错误?


归档时间:

查看次数:

37326 次

最近记录:

6 年,6 月 前