Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 & RxJS 6 Breaking Changes

I've google the crap out of this and I can't find a solution.

I've been using code like this for some time now. http is the angular HttpClient.

 forgotPassword(email: string): Observable<ApiReturn> {
        const url = `${this.apiURL}/ForgotPassword`;
        const params = {
            email
        };
        return this.http
            .post<ApiReturn>(url, params, this.requestOptions)
            .pipe(catchError(e => this.handleError(e)));
    }

I updated to the latest Angular 6.x version and RxJS 6 (from 5.5). Now the code is complaining about the catchError:

Argument of type 'OperatorFunction' is not assignable to parameter of type 'OperatorFunction'. Types of parameters 'source' and 'source' are incompatible. Type 'Observable' is not assignable to type 'Observable'.

My HttpInterceptor is also now failing to compile.

import { Injectable } from '@angular/core';
import {
    HttpEvent,
    HttpInterceptor,
    HttpHandler,
    HttpRequest,
    HttpResponse
} from '@angular/common/http';
import { Log, Level } from 'ng2-logger/client';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
    constructor() {
    }

    intercept(
        req: HttpRequest<any>,
        next: HttpHandler
    ): Observable<HttpEvent<any>> {
        // Get the auth header from the service.
        // const authHeader = this.global.authenticationToken;
        // Clone the request to add the new header.
        const authReq = req.clone({
            headers: req.headers
                .set('Access-Control-Allow-Origin', window.location.href)
        });
        // Pass on the cloned request instead of the original request.
        return next.handle(authReq);
    }
}

Error: [ts] Type 'import("c:/ProjDotNet/collegebowl-site/node_modules/@angular/core/node_modules/rxjs/internal/Observable").Observable>' is not assignable to type 'import("c:/ProjDotNet/collegebowl-site/node_modules/rxjs/internal/Observable").Observable>'. Types of property ' source' are incompatible.

Basically the same issue.

I gather I'm missing something basic in the pipe function but I can't figure it out or find an example that is doing what I am doing. Any help would be appreciated.

like image 801
Nick H Avatar asked Sep 02 '18 22:09

Nick H


People also ask

What is an Angular 6?

Angular 6 is a JavaScript framework for building web applications and apps in JavaScript, html, and TypeScript, which is a superset of JavaScript. Angular provides built-in features for animation, http service, and materials which in turn has features such as auto-complete, navigation, toolbar, menus, etc.

Is AngularJS and Angular 6 same?

One vital distinction between Angular vs AngularJS is AngularJS is JavaScript-based while Angular is TypeScript based. These two frameworks have similarities as a front end, open-source platform that create dynamic SPAs but let's look at their differences.

What is Angular v11?

Angular 11 - Features & Updates. The Google-developed web framework has come up with a new version as a production release. The highlight of this version is router performance improvements, automatic inlining of fonts and stricter types. With the router performance apps are made faster.

When was Angular 6 release?

Version 6. Angular 6 was released on May 4, 2018.


2 Answers

Like @meriton said, this is because you have multiple instance of rxjs in multiple node_modules. The best solution I've found is to add an alias into tsconfig.json to force the use of the same rxjs everywhere :

"compilerOptions": {
  "paths": {
      "rxjs": [
        "node_modules/rxjs"
      ],
      "rxjs/*": [
        "node_modules/rxjs/*"
      ]
   }
}
like image 167
thibaud Avatar answered Oct 23 '22 11:10

thibaud


Look more closely at the error message. It says that

import("c:/ProjDotNet/collegebowl-site/node_modules/@angular/core/node_modules/rxjs/internal/Observable").Observable>

and

import("c:/ProjDotNet/collegebowl-site/node_modules/rxjs/internal/Observable").Observable>

are different types. That is, you actually have two different kinds of Observable that come from separate copies of RxJS that reside in different directories in your file system.

That is, your node_modules is in a very weird state. Running npm ls rxjs or yarn why rxjs might yield clues as to why npm / yarn thought it a good idea to install RxJS twice.

like image 18
meriton Avatar answered Oct 23 '22 11:10

meriton