简体   繁体   English

TypeScript中的双重声明是什么意思?

[英]What does double type declarations in TypeScript mean?

I was doing the Angular tutorial here 我在这里做Angular教程

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. 所以错误被声明为类型any,然后有另一个冒号来声明一个以Observable作为参数的函数。 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>

(error: any): Observable<T>

After this you initialize your variable with a function which has body 在此之后,使用具有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> 所以你的handleError 返回一个带签名的函数,它带有一个类型为any的参数,并返回一个Observable<T>

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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