Skip to content

Simplify the Observable pipe with the help of Typescript Variadic Functions #7481

Open
@hansschenker

Description

@hansschenker

Describe the bug

// Define a type alias for variadic functions
type PipeFunctions = [(source: Observable) => any, ...Array<UnaryFunction<any, any>>];

class Observable {
// Simplified pipe method using variadic tuple types
pipe(...operations: PipeFunctions): R {
return _pipe(...operations)(this as any);
}
}


Explanation:

Type Alias for Variadic Functions:

PipeFunctions is a tuple type

  • where the first element is a function that takes an Observable and
    returns any,
  • followed by any number of UnaryFunction<any, any>.

Generic Pipe Method:

The pipe method is defined with a single generic parameter R to infer the return type based on the provided operations.

The method accepts a rest parameter ...operations typed as PipeFunctions.

Using _pipe Function:

Within the pipe method, the operations are passed to the _pipe function using the spread operator ...operations.

The result of _pipe(...operations) is then called with this (the current Observable instance), which effectively chains the operations together.

This approach significantly reduces the number of overload signatures required and leverages TypeScript's variadic tuple types to handle any number of operations in a type-safe manner.

Expected behavior

// Define a type alias for variadic functions
type PipeFunctions = [(source: Observable) => any, ...Array<UnaryFunction<any, any>>];

class Observable {
// Simplified pipe method using variadic tuple types
pipe(...operations: PipeFunctions): R {
return _pipe(...operations)(this as any);
}
}


Explanation:

Type Alias for Variadic Functions:

PipeFunctions is a tuple type

  • where the first element is a function that takes an Observable and
    returns any,
  • followed by any number of UnaryFunction<any, any>.

Generic Pipe Method:

The pipe method is defined with a single generic parameter R to infer the return type based on the provided operations.

The method accepts a rest parameter ...operations typed as PipeFunctions.

Using _pipe Function:

Within the pipe method, the operations are passed to the _pipe function using the spread operator ...operations.

The result of _pipe(...operations) is then called with this (the current Observable instance), which effectively chains the operations together.

This approach significantly reduces the number of overload signatures required and leverages TypeScript's variadic tuple types to handle any number of operations in a type-safe manner.

Reproduction code

// Define a type alias for variadic functions
type PipeFunctions<T> = [(source: Observable<T>) => any, ...Array<UnaryFunction<any, any>>];

class Observable<T> {
  // Simplified pipe method using variadic tuple types
  pipe<R>(...operations: PipeFunctions<T>): R {
    return _pipe(...operations)(this as any);
  }
}

------------------------
Explanation:

Type Alias for Variadic Functions:

PipeFunctions<T> is a tuple type 
- where the first element is a function that takes an Observable<T> and  
  returns any, 
- followed by any number of UnaryFunction<any, any>.

Generic Pipe Method:

The pipe method is defined with a single generic parameter R to infer the return type based on the provided operations.

The method accepts a rest parameter ...operations typed as PipeFunctions<T>.

Using _pipe Function:

Within the pipe method, the operations are passed to the _pipe function using the spread operator ...operations.

The result of _pipe(...operations) is then called with this (the current Observable instance), which effectively chains the operations together.

This approach significantly reduces the number of overload signatures required and leverages TypeScript's variadic tuple types to handle any number of operations in a type-safe manner.

Reproduction URL

No response

Version

whatever

Environment

yes

Additional context

yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions