简体   繁体   中英

What does double type declarations in TypeScript mean?

I was doing the Angular tutorial here

The following piece of code have double type declarations, but I don't understand what it means.

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);
   };
}

So error is declared as of type any, then there's another colon to declare a function with Observable as the parameter. What exactly is it returning?

You return a type which is a function . It takes an error: any and returns Observable<T> .

(error: any): Observable<T>

After this you initialize your variable with a function which has body

=> {
      console.error(error);

      this.log(`${operation} failed: ${error.message}`);

      return of(result as T);
};

So your handleError returns a function with signature taking a parameter of type any and returning an Observable<T>

(error: any): Observable<T>
return (error: any): Observable<T> => { ... }

返回一个函数,取一个名为error的参数,类型为any ,其返回类型为Observable<T> ,其主体位于花括号之间。

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