rxjs/Subject.d.ts error : Class 'Subject<T>' incorrectly extends base class 'Observable<T>'

AngularTypescriptRxjs

Angular Problem Overview


I extracted sample template code from this tutorial 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.

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

  3. I read through this github thread

  4. 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"
  }
}

Angular Solutions


Solution 1 - Angular

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.

Solution 2 - Angular

As others have pointed out this issue came about as a consequence of the stricter type checking of generics introduced in TypeScript 2.4. This exposed an inconsistency in a RxJS type definition and was consequently fixed in RxJS version 5.4.2. So ideal solution is to just upgrade to 5.4.2.

If you for some reason cannot upgrade to 5.4.2 you should instead use Alan Haddad's solution of augmenting the type declaration to fix it for your own project. I.e. add this snippet to your app:

// 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>;
  }
}

His solution will leave no other side-effects and is thus great. Alternatively proposed solutions have more side-effects for your project setup and thus are less ideal:

  • turn off the stricter generic checks with compiler flag --noStrictGenericChecks. This will however make it less strict for your own app, which you can do, but might introduce inconsistent type definitions like it did in this RxJS instance which in turn might introduce more bugs in your app.
  • Not checking the types in libraries with flag --skipLibCheck set to true. This is not ideal either as you might not get some type errors reported.
  • Upgrading to RxJS 6 Alpha - given that there are breaking changes this might break your app in badly documented ways, seeing as it's still in alpha. Additionally if you have other dependencies like Angular 2+ this might not really be a supported option, breaking the framework itself now or down the line. Which I think is an even harder issue to solve.

Solution 3 - Angular

An admitted band-aid approach is to add the following to your tsconfig.json file until the RxJS folks decide what they want to do about the error when using TypeScript 2.4.1 :

"compilerOptions": {

    "skipLibCheck": true,

 }

Solution 4 - Angular

You can temporarily work around the issue by using Module Augmentation

The following code resolved the issue for me. Once RxJS has a stable release that does not exhibit this issue, it should be removed.

// 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>;
  }
}

While it is perhaps ironic that RxJS itself makes such heavy use of this technique to describe its own shape, it is actually generally applicable to an array of problems.

While there are certainly limits and rough edges, part of what makes this technique powerful is that we can use it to enhance existing declarations making them more type safe without forking and maintain the entire file.

Solution 5 - Angular

"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"
}

in package.json "rxjs": "^5.4.2", and "typescript": "^2.3.4", then npm install and ng serve its working.

Solution 6 - Angular

Just a little bit of detail, in an attempt to put in my two cents.

The problem arises when we upgrade Typescript to the latest version, which by now was [TypeScript 2.4.1][1], [ "as we love the upgrades : )" ], and as [mentioned by @Jonnysai][2] in his answer and the link provided there, there is a detailed discussion regarding the problem and its fixes.

So, what worked for me was:

  1. I had to Uninstall TypeScript 2.4.1 first, by going to Control Panel > Programs and Features...
  2. Install, afresh, [TypeScript 2.4.0][3], and then make sure, in package.json file, you have this

    [![enter image description here][4]][4]
    configuration, as mentioned [here][5] in a rather shorter detail.

    You can download TypeScript 2.4.0 from [here][3], for Visual Studio 2015

[1]: http://download.microsoft.com/download/6/D/8/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA/2.4.1-TS-release-dev14update3-20170626.1/TypeScript_Dev14Full.exe "TypeScript 2.4.1" [2]: https://stackoverflow.com/a/44793989/1042705 "Jonnysai's answer" [3]: http://download.microsoft.com/download/6/D/8/6D8381B0-03C1-4BD2-AE65-30FF0A4C62DA/2.4.0-TS-release-dev14update3-20170608.3/TypeScript_Dev14Full.exe "TypeScript 2.4.0 Download" [4]: https://i.stack.imgur.com/QNMHs.jpg [5]: https://stackoverflow.com/a/44858294/1042705 "StackOverflow | Class Subject<T> incorrectly extends base class Observable<T>"

Solution 7 - Angular

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: https://stackoverflow.com/questions/44810195/how-do-i-get-around-this-error-in-rxjs-5-x-in-typescript-2-4/44810196#44810196

Solution 8 - Angular

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

Solution 9 - Angular

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>

Solution 10 - Angular

Keep the noStrictGeneric option true in tsconfig.config file

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

Solution 11 - Angular

I found same problem and I solved it by using "rxjs": "5.4.1", "typescript": "~2.4.0" and adding "noStrictGenericChecks": true into tsconfig.json.

Solution 12 - Angular

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.

Solution 13 - Angular

GO to Package.json file and modify the below configuration

...
  "rxjs": "5.4.1",
  "typescript": "~2.4.0"
...

It will work

Solution 14 - Angular

Using the above alone did not help, however, using the following approach: How do I get around this “Subject incorrectly extends Observable” error in TypeScript 2.4 and RxJs 5

solved the issues. It is also mentioned there that the issue has been fixed in RxJs 6, so this is more of a temporary fix, which helped me in successfully running this great example (which compiled before but gave the error during load time): Angular 4 application development with Bootstrap 4 and TypeScript

Solution 15 - Angular

reinstalling rxjs -> npm install rxjs@6 rxjs-compat@6 --save

Solution 16 - Angular

Now we dont need the ionic-native module - we only need @ionic-native/core.

Run npm uninstall ionic-native --save

It will solve Your Problem..

Solution 17 - Angular

just add

"noStrictGenericChecks": true

to your tsconfig.json

Solution 18 - Angular

thanks the answer of zmag and Rahul Sharma, it works! @zmag @Rahul Sharma

  "rxjs": "5.4.1",
  "typescript": "~2.4.0"

my problem is as follow:

typings/globals/core-js/index.d.ts:3:14 - error TS2300: Duplicate identifier

Make changes as.

GO to Package.json file and modify the below configuration

  "rxjs": "5.4.1",
  "typescript": "~2.4.0"

Solution 19 - Angular

yes the problem was with TypeScript 2.4.1 I just uninstalled this from Control panel and it works for me as Visual Studio 2017 already has this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeepak SView Question on Stackoverflow
Solution 1 - AngularCharanRootView Answer on Stackoverflow
Solution 2 - AngularKoslunView Answer on Stackoverflow
Solution 3 - Angularuser3785010View Answer on Stackoverflow
Solution 4 - AngularAluan HaddadView Answer on Stackoverflow
Solution 5 - AngularKetan ChaudhariView Answer on Stackoverflow
Solution 6 - AngularIrfView Answer on Stackoverflow
Solution 7 - AngularDaniel RosenwasserView Answer on Stackoverflow
Solution 8 - AngularAkshay BagiView Answer on Stackoverflow
Solution 9 - Angularuser8709803View Answer on Stackoverflow
Solution 10 - AngularJigar TannaView Answer on Stackoverflow
Solution 11 - AngularKhachornchit SongsaenView Answer on Stackoverflow
Solution 12 - AngularSuresh MediboyinaView Answer on Stackoverflow
Solution 13 - AngularRahul SharmaView Answer on Stackoverflow
Solution 14 - AngularGil ShapirView Answer on Stackoverflow
Solution 15 - AngularYakup AdView Answer on Stackoverflow
Solution 16 - AngularShubham PandeyView Answer on Stackoverflow
Solution 17 - AngularMiguel AfonsoView Answer on Stackoverflow
Solution 18 - AngularSageXView Answer on Stackoverflow
Solution 19 - AngularSubhash SharmaView Answer on Stackoverflow