ReactiveX / ReactiveX/rxjs

Simplify the Observable pipe with the help of Typescript Variadic Functions

Open
#7,481 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
31.7k
Forks
3k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Observable.pipe implementation and its overload signatures; the issue names no file or test. Compare the proposed variadic TypeScript typing with the existing public API and type-checking coverage, then verify that supported pipe chains retain correct inferred types.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.