Home > Net >  How to create custom rxjs operator with destructured parameters
How to create custom rxjs operator with destructured parameters

Time:12-15

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

CodePudding user response:

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

CodePudding user response:

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
    })
  )
}
  • Related