类型“{}”不可分配给类型 'HttpEvent<any>'

Type '{}' is not assignable to type 'HttpEvent<any>'

我有一个错误,我找不到如何解决它,我有一个拦截器,它在令牌过期时获取状态,我继续刷新令牌问题是当我提出 Angular project 在控制台提示错误,在(return nex .handle(this.addToken(req)).pipe)这一行错误如下。

提前感谢您的帮助。

ERROR in src/app/auth-interceptor.ts(83,19): error TS2322: Type 'Observable<{} | HttpProgressEvent | HttpSentEvent | HttpHeaderResponse | HttpResponse<any> | 
Http...' is not assignable to type 'Observable<HttpEvent<any>>'.
Type '{} | HttpProgressEvent | HttpSentEvent | HttpHeaderResponse | HttpResponse<any> | HttpUserEvent<a...' is not assignable to type 'HttpEvent<any>'.     
Type '{}' is not assignable to type 'HttpEvent<any>'.
Type '{}' is not assignable to type 'HttpUserEvent<any>'.
        Property 'type' is missing in type '{}'.
return next.handle(this.addToken(req)).pipe(
                    catchError((error: HttpEvent<any>) => {
                        if (error instanceof HttpErrorResponse) {
                          console.log("error ",error);
                            switch ((<HttpErrorResponse>error).status) {
                              case 400:
                                return this.handle400Error(error);
                              case 403:
                                  return this.handle403Error(req, next); 
                              default:
                                  return throwError(error);
                            }
                        } else {
                            return throwError(error);
                        }
                    }));

看来问题出在 return 类型的 handle400Errorhandle403Error 方法中。 根据 catchError 文档:

Catches errors on the observable to be handled by returning a new observable or throwing an error.

@return {Observable} An observable that originates from either the source or the observable returned by the catch selector function.

这意味着,catchError 的结果应该是一个错误,或者 HttpEvent<any>(源类型)可观察到。

例如以下代码片段没有类型错误:

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError((error: Error) => {
        if (error instanceof HttpErrorResponse) {
          switch ((<HttpErrorResponse>error).status) {
            case 400:
              return this.handle400Error(error);
            default:
              return throwError(error);
          }
        } else {
          return throwError(error);
        }
      }));
  }

  handle400Error(error: HttpErrorResponse) {
    return of(new HttpResponse());
  }

注意:HttpEvent<any>类型与HttpErrorResponse类型无关,所以catchError.[=22=中的错误最好有Error类型]