How to pass an array of pipable operators to pipe()?

Discussão em 'Angular' iniciado por Yegor Petrov, Dezembro 21, 2024.

  1. Yegor Petrov

    Yegor Petrov Guest

    [Angular 12]I have a service with a method, handleFormControlChanges, which accepts, a formControl, a destroy subject, a method and an array of pipable operators (e.g., delay), and I would like to apply them using pipe().

    handleFormControlChanges<T>(
    formControl: FormControl,
    destroy$: Subject<void>,
    onValueChange: (value: T) => void,
    customOperators: Array<OperatorFunction<any, any>> = []
    ): void {

    formControl.valueChanges
    .pipe(
    ...customOperators,
    takeUntil(destroy$),
    tap(() => this.setLoading$(true))
    )
    .subscribe(onValueChange);
    }


    I can't just destructure an array like ...customOperators and pass it to pipe() as it causes this TypeScript error:

    TS2575: No overload expects 2 arguments, but overloads do exist that expect either 2 or 4 arguments.

    How can I pass the array of pipable operators to pipe()? How can I avoid this error?

    Continue reading...

Compartilhe esta Página