Why is it necessary to use "result as T" in the return of a generic method?

Asked

Viewed 99 times

0

Consider the following method:

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

whereas the implementation of a generic type implies a return of the same generic type T, why the need to use the result as T, whereas result will always be the type T?

return of(result as T);
  • What is this of?

  • @Maniero is a method of Observable, that belongs to the Rxjs library used in Angular.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.