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
The weird thing is that I can let the error go away if I comment out one of the operators
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));