rxjs/Subject.d.ts error : Class 'Subject

2019-01-09 00:30发布

I extracted sample template code from https://github.com/gopinav/Angular-2-Tutorials and did below two steps to get started -

  1. npm install // worked fine and created node_modules folder with all dependencies
  2. npm start // failed with below error-

    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
    

I see that in the subject.d.ts declaration of lift is as below -

 lift<T, R>(operator: Operator<T, R>): Observable<T>;

And in Observable.ts it is defined as below-

 lift<R>(operator: Operator<T, R>): Observable<R> {

Note:- 1. I am new to Angular2 and trying to get hold of things.

  1. The error might be due to incompatible definitions of lift method

  2. I read through https://github.com/Microsoft/TypeScript/issues/2073

  3. If I need to install some different version of rxjs then please tell how to uninstall and install the correct rxjs.

Edit1: I might be a bit late in responding here but I still get the same error even after using typescript 2.3.4 or rxjs 6 alpha. Below is my 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"
  }
}

16条回答
你好瞎i
2楼-- · 2019-01-09 00:34

As per this you need update typescript 2.3.4 or rxjs 6 alpha

go the your package.json file in your project update typescript or rxjs version. Example

"typescript": "2.3.4"

do npm install

update(06/29/2017):-

As per the comments typescript "2.4.0" working.

查看更多
The star\"
3楼-- · 2019-01-09 00:34

RxJS 6 will have this fixed, but as a temporary workaround, you can use the noStrictGenericChecks compiler option.

In tsconfig.json, put it in compilerOptions and set it to true.

{
    "compilerOptions": {
        "noStrictGenericChecks": true
    }
}

On the command line it's --noStrictGenericChecks.

Why it's happening:

TypeScript 2.4 has a strictness change, and Subject isn't lifting to the correct Observable. The signature really should have been

(operator: Operator) => Observable

This will be fixed in RxJS 6.

查看更多
孤傲高冷的网名
4楼-- · 2019-01-09 00:35

Keep the noStrictGeneric option true in tsconfig.config file

{
    "compilerOptions": {
        "noStrictGenericChecks": true
    }
}
查看更多
We Are One
5楼-- · 2019-01-09 00:37

You can temporarily use the --noStrictGenericChecks flag to get around this in TypeScript 2.4.

This is --noStrictGenericChecks on the command line, or just "noStrictGenericChecks": true in the "compilerOptions" field in tsconfig.json.

Keep in mind, RxJS 6 will have this corrected.

See this similar question: How do I get around this error in RxJS 5.x in TypeScript 2.4?

查看更多
对你真心纯属浪费
6楼-- · 2019-01-09 00:37

Please change the following line of Subject.d.ts file:

lift<R>(operator: Operator<T, R>): Observable<T>;

to:

lift<R>(operator: Operator<T, R>): Observable<R>;

The return type should probably be Observable<R> rather than Observable<T>

查看更多
ら.Afraid
7楼-- · 2019-01-09 00:45

Add "noStrictGenericChecks": true in tsconfig.json file under "compilerOptions" node as shown in below image & build application. I have faced same error after adding this error resolved. enter image description here

查看更多
登录 后发表回答