简体   繁体   English

如何使用解构参数创建自定义 rxjs 运算符

[英]How to create custom rxjs operator with destructured parameters

I'm trying to create a custom RxJS filter operator that takes as a parameter destructured array.我正在尝试创建一个自定义 RxJS 过滤器运算符,它将解构数组作为参数。

But it seems that typescript is not happy with the type, I'm getting this error:但似乎 typescript 对该类型不满意,我收到此错误:

TS2493: Tuple type '[]' of length '0' has no element at index '0'. TS2493:长度为“0”的元组类型“[]”在索引“0”处没有元素。


export function customOperator<T extends []>() {
    return pipe(
        filter<T>(([param1, param2, param3])=> {
            //some logic
        })
    );
}

You can use the rest operator (...) to unpack the array and then destructure the items.您可以使用剩余运算符 (...) 来解压缩数组,然后解构项目。 This will allow the compiler to understand that you'll be accessing items in the array.这将使编译器了解您将访问数组中的项目。

export function customOperator<T extends []>() {
return pipe(
    filter<T>(([...[param1, param2, param3]])=> {
        //some logic
    })
);
}

One way to solve the issue is to use rest operator to destructure the array argument解决该问题的一种方法是使用 rest 运算符来解构数组参数

Code:代码:

export function customOperator<T extends []>() {
  return pipe(
    filter<T>((...params)=> {
      //some logic
    })
  )
}

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

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