Home > Net >  Partial<Observer<unknown>> error when trying to type next value in subscribe
Partial<Observer<unknown>> error when trying to type next value in subscribe

Time:03-11

I've created a rxjs stream with a couple of operators in it, which I will show you in a bit, but first I would like to describe the error

I have the following (simplified)

return fromEvent(....).pipe(
  ....
).subscribe((output: IOutput<IData>) => ....); // <-- error

enter image description here

The weird thing is that I can let the error go away if I comment out one of the operators

enter image description here

Here I commented out the tap operator, but the same thing happens when I comment out that filter operator.

Another way to fix this, is to rewrite the subscribe into

 .subscribe((output)  => observer.next(output as IOutput<IData>));

But whatever I do, I no clue what is going on here. For example, how can commenting out that tap operator solve the error.

Any help would be appreciated!

CodePudding user response:

The problem doesn't show up on StackBlitz due to the tsconfig settings (probably strictFunctionTypes), but you can reproduce it in a TypeScript playground.

The issue is that the rxjs type definitions only type pipe overloads for up to 10 arguments. Beyond 10, the result type becomes Observable<unknown>.

You can fix it by chaining pipes together once you reach ten operators:

const sub = fromEvent<Event>(document, 'click', { capture: true })
  .pipe(
    xPreventPropagation$<Event>(),
    ..
    tap((update) => console.log('test3')))
  .pipe(
    map<IData, IOutput<IData>>((update: IData) => ({
      result: update,
    }))
  )
  .subscribe((output: IOutput<IData>) => observer.next(output));

TypeScript playground

  • Related