简体   繁体   English

输入自定义 RxJS 运算符

[英]Typing a Custom RxJS Operator

I have a custom RxJS operator that map s data that comes back from our API. Here's the Operator:我有一个自定义的 RxJS 运算符,它从我们的 API 返回map的数据。这是运算符:

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.我想输入ActionResult<any>但我不确定该怎么做。 The ActionResult interface takes a generic to type it properly, so I'm hoping to be able to type this operator as well. ActionResult接口采用泛型来正确键入它,因此我希望也能够键入此运算符。

A friend on Twitter gave me the following answer , which is correct and works: Twitter 上的一位朋友给了我以下答案,它是正确的并且有效:

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

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

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