pjlamb12
pjlamb12

Reputation: 2422

Typing a Custom RxJS Operator

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.

Upvotes: 1

Views: 331

Answers (1)

pjlamb12
pjlamb12

Reputation: 2422

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

Upvotes: 2

Related Questions