简体   繁体   中英

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.

But it seems that typescript is not happy with the type, I'm getting this error:

TS2493: Tuple type '[]' of length '0' has no element at index '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

Code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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