Home > Net >  How to type a rest parameter that takes an array of rxjs operators?
How to type a rest parameter that takes an array of rxjs operators?

Time:09-20

I'm trying to find a way to type a rest parameter that takes an array of rxjs operators, which are essentially observables.

So far this is the implementation i have. Typescript isn't complaining but this isn't typed good enough.

testFunction(...args: any[]):BehaviorSubject<any>{
   this.selector.pipe(...args, delay(1000)).subscribe();
}

The function should be able to take an infinite number of rxjs operators. Ex:

testFunction(take(1), skip(2), ...);

How would i be able to express that in typescript ?

CodePudding user response:

The pipe() function is expecting UnaryFunction<any, any> parameters:

testFunction(...args: UnaryFunction<any, any>[]): BehaviorSubject<any>{
   this.selector.pipe.apply(this.selector, ...args, delay(1000)).subscribe();
}

You can't really do better than that (typing the BehaviorSubject for instance) without overloading several times your own function, depending on the number of parameters, as it's done with pipe in RxJS.

  • Related