简体   繁体   中英

Argument of type '{}' is not assignable to parameter of type in Angular 8

I have a method for handling errors:

private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error);
      this.log(`${operation} failed: ${error.message}`);

      return of(result as T);
    };
  }

  private log(message: string) {
    console.log(message);
  }

and I want to use it inside my method:

getMyStructure(): Observable<HttpResponse<StructureResponse>> {
    return this.http.get< StructureResponse >(
        localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}

but the last {} throws me an error. The StructureResponse is an Interface with some fields. In my understanding I should put there some (empty?) object of StructureResponse but I'm no longer sure. Can you help me with that?

this is the StructureResponse:

interface SomeStructure {
  content: MyStructure[];
  page: number;
  size: number;
  totalElements: number;
  totalPages: number;
  last: boolean
}

Add ? to each property.

interface SomeStructure {
  content?: MyStructure[];
  page?: number;
  size?: number;
  totalElements?: number;
  totalPages?: number;
  last?: boolean
}

Use Observable<StructureResponse> instead Observable<HttpResponse<StructureResponse>> as below.

getMyStructure(): Observable<StructureResponse> {
    return this.http.get< StructureResponse >(
        localUrl).pipe(retry(3), catchError(this.handleError<StructureResponse>('getMyStructure', {})));
}

如果您传递一个空对象,那么它分配给接口的唯一方法是接口中的属性都是可选的(带有?),否则您至少需要在对象中使用 null 或其他方式显示您的属性价值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM