Typing a Custom RxJS Operator

Viewed 72

I have a custom RxJS operator that maps data that comes back from our API. Here's the Operator:

export function mapActionResult(): OperatorFunction<ActionResult<any>, any> {
    return map((data: ActionResult<any>) => data.result);
}

// in use:
getData() {
  return this._http.get(this.someUrl).pipe(
    mapActionResult()
  )
}

I would like to get the ActionResult<any> typed but am not sure how to do that. The ActionResult interface takes a generic to type it properly, so I'm hoping to be able to type this operator as well.

1 Answers

A friend on Twitter gave me the following answer, which is correct and works:

export function mapActionResult<T>(): OperatorFunction<ActionResult<T>, T> {
    return map((data: ActionResult<T>) => data.result);
}
Related