Home > Mobile >  What will be the return type of function?
What will be the return type of function?

Time:01-05

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 {...}

CodePudding user response:

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

CodePudding user response:

Thank you... its working for me.

  •  Tags:  
  • Related