Rxjs error type 'Observable<HttpEvent<any>>'

Andrew Junior Howard

I've recently upgraded my rxjs packages. However my Angular interceptor is now erroring. Here is my code:

import { HttpEvent, HttpHandler, HttpResponse, HttpInterceptor, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Observable, tap, catchError } from 'rxjs';
import { Injectable } from '@angular/core';
import { BusyService } from './shared/services/busy.service';
import { Router } from "@angular/router";

@Injectable()
export class ProgressInterceptor implements HttpInterceptor {

    

    constructor(private router: Router, private busyService: BusyService) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        

        return next.handle(req).pipe(
            tap((event: HttpEvent<any>) => {
                

                // Only enable and disable buttons if the skip interceptor was NOT set as a header on that call
                if ((req.headers.get('X-Skip-Interceptor') != "True")) {

                    

                    // If request was sent
                    if ((event.type == 0)) {
                        //console.log("busy");
                        this.busyService.changeBusy(true);
                    }
                    else {
                        //console.log("not busy");
                        this.busyService.changeBusy(false);
                    }

                }

            }),
            catchError((err: HttpErrorResponse) => {

                

                let errorReceived;

                if ((typeof err == 'string'))
                {
                    errorReceived = JSON.parse(err);
                }
                else
                {
                    errorReceived = err;
                }

                this.busyService.changeBusy(false);
                

                return Observable.throw(err);
            }));

        
    }
}

Here is the error:

Error: src/app/progress.interceptor.ts:17:9 - error TS2322: Type 'Observable<unknown>' is not assignable to type 'Observable<HttpEvent<any>>'.
  Type 'unknown' is not assignable to type 'HttpEvent<any>'.
    Property 'type' is missing in type '{}' but required in type 'HttpUserEvent<any>'.

 17         return next.handle(req).pipe(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 18             tap((event: HttpEvent<any>) => {
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
 65                 return Observable.throw(err);
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 66             }));
    ~~~~~~~~~~~~~~~~

  node_modules/@angular/common/http/http.d.ts:3861:5
    3861     type: HttpEventType.User;
             ~~~~
    'type' is declared here.


Error: src/app/progress.interceptor.ts:65:35 - error TS2339: Property 'throw' does not exist on type 'typeof Observable'.

65                 return Observable.throw(err);

There is zero information out there that I can find to update the intercept code. The error isn't even helpful as it's referencing a node module file that I shouldn't edit.

Michael D

From which RxJS version did you migrate from? In the recent versions, Observable.throw has been replaced with throwError.

  1. You'd need to make the following changes to the RxJS import statement(s)
import { Observable, throwError } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';

And replace Observable.throw() with throwError

catchError((err: HttpErrorResponse) => {
  ...
  return throwError(err);
}
  1. Assert the type using Typescript as
intercept(
  req: HttpRequest<any>, 
  next: HttpHandler
): Observable<HttpEvent<any>> {
  return next.handle(req).pipe(
    ...
  ) as Observable<HttpEvent<any>>;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error getting token on interceptor - Type 'Promise<void | Observable<HttpEvent<any>>>' is not assignable to type 'Observable<HttpEvent<any>>'

Type 'Observable<void>' is not assignable to type 'Observable<HttpEvent<any>>' Type 'void' is not assignable to type 'HttpEvent<any>'

Angular interceptor - Type 'Observable<unknown>' is not assignable to type 'Observable<HttpEvent<any>>'

Error: Type 'void' is not assignable to type 'HttpEvent<any>'

Property 'catchError' does not exist on type 'Observable<HttpEvent<any>>'

Squashing Promise<Observable<HttpEvent<any>>> to Observable<HttpEvent<any>>

Observable HttpEvent is not assignable to type Observable in Typescript

Type 'Observable<HttpEvent<T>>' is not assignable to type 'Observable<T>'

Angular argument of type 'HttpEvent<any>' is not assignable to parameter

Angular/RxJS type error when mapping observable

Specify type of rxjs observable's "error" handler

Property 'connect' does not exist on type 'Observable<any>' | RXJS multicast

[Typescript][RxJS] Why is there no type error when returning an observable of partial type?

Angular HttpClient return expecting observable<HttpEvent<any> rather than observable<any>

RxJs observable: transforming data into enum causes a type error

Rxjs : [ts] Property 'pipe' does not exist on type 'UnaryFunction<Observable<{}>, Observable<string | {}>>'. any

Error constructing an rxjs/Observable

Type 'Observable<{}>' is not assignable to type 'Observable<Item[]>: Rxjs

Type '() => Observable<any>' is not assignable to type '() => Observable<any>'

Type 'Observable<any[] | Observable<any[]>>' is not assignable to type 'Observable<any[]>'

RXJS Observable enum type, Type 'Observable<Type>' is not assignable to type 'Type'

RxJS Type signature of EMPTY observable?

Rxjs type Observable<Unknown> is not assignable to Observable<void>

Is the truly actual type of error in Observable class of Rx really any?

error TS2322: Type 'Observable<any>' is not assignable to type 'string | string[]'. Type 'Observable<any>' is missing the following properties

Type Observable<Observable<any[]>> is not assignable to type Observable<any[]>

Type 'Observable<any>' is not assignable to type '[]'

HttpParams use with Put and Post change return type to Observable<HttpEvent<T>> in Angular 6

Error rxjs_Observable__.Observable.forkJoin is not a function?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive