简体   繁体   中英

What will be the return type of function?

export interface IResponseCount {
  count: number;
  status?: number;
  success: boolean;
}



extractResponse() {
    return (source: Observable<IResponseCount>) => {
      return source.pipe(
        map(value => {
          if (!value.success) {
            throw new Error(..);
          }
          return value.count;
        })
      );
    };
  }

What will be the return type of function extractResponse. Ex. extractResponse(): SOMETHING {...}

Based on your provided code the return type is following:

(source: Observable<IResponseCount>) => Observable<number>

Because your function returns a function which takes an Observable and returns an Observable which provides a number.

So, your code would look like this with the return type:

extractResponse(): (source: Observable<IResponseCount>) => Observable<number> {
  return (source: Observable<IResponseCount>) => {
    return source.pipe(
      map(value => {
        if (!value.success) {
          throw new Error('..');
        }
        return value.count;
      })
    );
  };
}

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