Skip to content

Recursive Variadic Tuples not working for functional pipelines #62479

Closed as not planned

Description

@dariomannu

🔎 Search Terms

For a long time, reactive libraries including RxJS lacked a proper typing solution for pipelines.

A pipeline is an array of 0 or more operator functions through which data flows through.

const stream = pipeline<I, O>(
  operator1,
  operator2,
  operatorn,
);

Typing such a stream with I and O for the input and output types respectively, means each operator function needs to have the proper types inferred and/or enforced, too:

const stream = pipeline<I, O>(
  operator1<I, T1>,
  operator2<T1, T2>,
  ...
  operatorn<TN, O>,
);

So, the first operator is typed I => T1, and so on, with intermediate types, until the last one has TN => O

Multiple attempts have been made by different people and LLMS to find a way, including the use of recursive and variadic types, but with no luck so far.
My understanding is it should be feasible with variadic tuples, so I suspect there might be a TS bug.

Some older unsuccessful attempts made so far:

export type OperatorPipeline<I, O=I> = 
  [] |
  [OperatorFunction<I, infer M>, ...OperatorPipeline<M, O>] |
  [OperatorFunction<I, O>]
;

export type OperatorPipeline<I, O=I> =
  [] |
  [OperatorFunction<I, O>] |
  [I, O] extends [infer FIRST, ...infer REST] 
    ? FIRST extends OperatorFunction<I, infer M>
      ? REST extends OperatorFunction<M, O>
        ? [OperatorFunction<I, M>, ...OperatorPipeline<M, O>]
        : never
      : never
    : never
;

export type OperatorPipeline<I, O, Ops extends OperatorFunction<any, any>[] = []> =
	Ops extends []
		? []                                                    // 0 operators
		: Ops extends [OperatorFunction<I, infer Next>]         // 1 operator
			? [OperatorFunction<I, Next>] & (Next extends O ? unknown : never)
			: Ops extends [OperatorFunction<I, infer Next>, ...infer Rest] // 2+ operators
				? Rest extends OperatorFunction<any, any>[]
					? [OperatorFunction<I, Next>, ...OperatorPipeline<Next, O, Rest>]
					: never
				: never
;

Related:
ReactiveX/rxjs#7481

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

No response

💻 Code

// Your code here

🙁 Actual behavior

TS throws various errors when parsing any of the above, and variants. Not listing them all, as we don't actually know whether this is a bug or a missing feature yet.

🙂 Expected behavior

Variadic tuples should help typing a whole operator pipeline, every step of the way.

Additional information about the issue

No response

Activity

  1. nmain commented on Sep 23, 2025

    @nmain

    This is either a duplicate of or very closely related to #30370, #25660, #12512, and/or #10247.

    Perhaps there's some new ground covered in this ticket that those don't, but you should establish what that difference is first.

    My search terms were functional pipe variadic.

  2. jcalz commented on Sep 23, 2025

    @jcalz
    Contributor

    Yeah, this looks like #30370 to me. My inclination here (given that I'm not 100% sure what your example means, so I'm guessing) would be to write

    type OperatorFn<I, O> = (i: I) => O
    type OperatorChain = [OperatorFn<any, any>, ...OperatorFn<any, any>[]];
    type CollapseChain<T extends OperatorChain> =
        T extends [OperatorFn<infer I, any>, ...any, OperatorFn<any, infer O>] ?
        OperatorFn<I, O> : T[0]
    
    type VerifyChain<T extends OperatorChain> = { [J in keyof T]:
        [unknown, ...T] extends Record<J, OperatorFn<any, infer I>> ?
        OperatorFn<I, T[J] extends OperatorFn<any, infer O> ? O : never> :
        T[J]
    }
    
    function pipeline<T extends OperatorChain>(...args: VerifyChain<T>): CollapseChain<T>;
    function pipeline(...args: OperatorChain) {
        return (a: any) => args.reduce((a, f) => f(a), a)
    }

    And this gives you behavior like

    const p = pipeline((a: string) => a.length, (n: number) => n > 3, (b: boolean) => b ? new Date() : new Date(0));
    // const p: OperatorFn<string, Date>
    
    pipeline((i0: string) => i0.length, (i1: string) => i1) // error! 
    //                                  ~~~~~~~~~~~~~~~~~~
    // Type 'number' is not assignable to type 'string'.(2345)

    Playground link

    It's not clear to me what more is needed here.

  3. RyanCavanaugh commented on Sep 23, 2025

    @RyanCavanaugh
    Member

    Generally people want a call like

    pipeline((s0: string) => s0.length, n1 => n1.toFixed(), s2 => s2.length, n3 => n3 * n3, n4 => n4.toString())

    to get inferred types for each parameter, but this requires an unbounded number of inference passes, whereas we usually don't want algorithms that can do unbounded amounts of work since structural types can and do result in infinite recursion that's sometimes quite difficult to detect.

    #30134 tracks completely changing how inference works but it's unlikely to happen.

  4. jcalz commented on Sep 23, 2025

    @jcalz
    Contributor

    In cases like this I usually give up on variadic stuff and just use method chaining instead, to get better inference:

    interface Pipeline<I, O> {
        run(i: I): O;
        and<U>(f: (o: O) => U): Pipeline<I, U>
    }
    const pipeline = <I, O>(run: (i: I) => O): Pipeline<I, O> => ({
        run,
        and: f => pipeline(i => f(run(i)))
    });
    
    const p = pipeline((s0: string) => s0.length)
        .and(n1 => n1.toFixed())
        .and(s2 => s2.length)
        .and(n3 => n3 * n3)
        .and(n4 => n4.toString());
    
    console.log(p.run("abcdefghij")); // "4"

    Playground link

  5. typescript-bot commented on Sep 26, 2025

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. locked as resolved and limited conversation to collaborators on Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions