Refine subscribe and tap signatures
Currently we have a variety of ways you can use subscribe and tap:
o.subscribe(fn, fn, fn);
o.subscribe(fn, fn);
o.subscribe(fn);
o.subscribe(void, void, fn);
o.subscribe(void, fn, fn);
o.subscribe(void, fn);
o.subscribe(observer);
o.pipe(tap(fn, fn, fn));
o.pipe(tap(fn, fn));
o.pipe(tap(fn));
o.pipe(tap(void, void, fn));
o.pipe(tap(void, fn, fn));
o.pipe(tap(void, fn));
o.pipe(tap(observer));
It's been my personnel recommendation that people stick to the following two signatures:
o.subscribe(fn);
o.subscribe(observer);
// or likewise with tap:
o.pipe(tap(fn));
o.pipe(tap(observer));
The reason being is:
// POOR readability (I have to count arguments?)
o.subscribe(null, null, () => {
// side effect here
});
// BETTER readability
o.subscribe({
complete() {
// side effect here
}
});
// GOOD readability
o.subscribe(x => {
// side effect here
});
// GOOD readability (if unnecessarily verbose)
o.subscribe({
next(x) {
// side effect here
}
});
Proposal
I'd like to deprecate the other signatures to subscribe and tap, and narrow down the signature to just those two prescribed above for the next major version
Pros
- Simplify subscription code slightly.
- Simpler typing for TypeScript.
- Guides people into writing cleaner, more readable code
Cons
- The behavior around handling errors at the point of subscription may get a slightly harder to control, with only one way of adding error handling at subscription time. (NOTE: errors should really be handled by a
catchErrororretry-type operator, rather than in the subscribe.)
Other thoughts
It would also be good to add some lint rules around this to rxjs-tslint and the like.
I think we can have tslint rules easily, that's probably not a problem.
For librarywise, I'd like to think bit more: specifically, you'd like to allow form of
subscribe(next);
but do not allow any other forms? or it is still allowed
subscribe(next, error);
subscribe(next, error, complete);
form?
but do not allow any other forms? or it is still allowed
It would be allowed in the short term, but in the long term (next major version or some future major version), it would be a hard API change.
And if anyone is wondering "why not just subscribe(observer) and forEach(fn)?"... Well, forEach is supposed to have the semantic of nexting on a microtask, and forEach returns a promise and is currently non-cancellable.
I'd expect that .subscribe(fn) is probably the 90% use case for people.
I'd expect that .subscribe(fn) is probably the 90% use case for people.
I agree it's major usecase, but not sure about 90%. i.e these two form
subscribe(next, error);
subscribe(next, error, complete);
I've been used all time, personally I never used observer object form (.subscribe({next: ...})).
I don't have strong opinion around readability between observer form vs. fn arguments, but does not allowing fn form except next bitters me, bit downvote around those decisions.
what if we allow fn form as is, only disallow optional form?
subscribe(null, error); //meh, not allowed
subscribe(next, error); //allowed
I'm all for this change; in fact I've recently started doing subscribe({ error }) instead of subscribe(null, error) simply because I found out that you could even do the former. I think that this deprecation will coach people into what I see as a better way of writing subscription code.
I'd like to discuss the "Con" a bit more:
The behavior around handling errors at the point of subscription may get a slightly harder to control...
Do you have more specifics about this? It seems like .subscribe({ error }) is not harder than .subscribe(null, error), but I'm not sure if I'm missing something.
...errors should really be handled by a catchError or retry-type operator, rather than in the subscribe.
I don't fully agree with this for a couple of reasons:
- We need a way to handle errors that happen in
next. - This requires a particular way of thinking about Observables -- specifically that everything flows through to the subscription handler and caught errors need to emit something that can be handled by that logic. Are you suggesting that we always want that to be the case?
I honestly could not agree more. With our work with RxJS, we've had so many bike shedding discussions due to this issue and the students I've helped teach RxJS often get caught on this issue (of error handling/syntax) based on the numerous ways to approach things and the different (usually 3rd party) resources.
What I specifically like about observable.subscribe(next, error) is that it's a nice parallel to promise.then(result, error). Although since the semantics are different, maybe that's an argument for getting rid of this "faux amis" situation. Okay, I've convinced myself that I agree with this proposal.
I agree with this. I found having the next, error, and complete handlers becomes difficult to read. Moved to pipe approach for everything with v5 and v6.
From the core team meeting:
Start with deprecating signatures like null, null, fn. Perhaps add some tslint rules to help guide people. Maybe move on to deprecating fn, fn, fn... @kwonoj is the voice of dissent.
BTW, there is a TSLint rule in rxjs-tslint-rules - named rxjs-prefer-observer - that relates to this issue.
Although I agree that observer syntax is a bit more readable, it is not always a win in practice. Consider the following code:
class A {
private status = false;
private subscription: Subscription | null = null;
constructor(private readonly obs: Observable<any>) {}
someMethod() {
this.subscription = this.obs.subscribe(value => {
// notice the usage of this here
this.status = true;
}, err => {
// some error handling
});
}
}
When attempting to transform this code to observer syntax, it fails on unavailability of "this".
Moreover, readability in a good editor (like IntelliJ Idea) is better, because the IDE shows parameter names. It looks like this:
class A {
private status = false;
private subscription: Subscription | null = null;
constructor(private readonly obs: Observable<any>) {}
someMethod() {
this.subscription = this.obs.subscribe(next: value => {
// notice the usage of this here
this.status = true;
}, error: err => {
// some error handling
});
}
}
I would appreciate any suggestions how to transform this code to observer syntax with retaining availability of "this". If that is not easily possible, I'd vote for un-deprecating these methods.
@sebek64 The only difference between your second snippet and the observer syntax is two enclosing braces. Well, that and that fact that your snippet isn't valid TypeScript.
@cartant Yes, it isn't a valid typescript. It is how the code is displayed by Idea. The added argument names "next" and "error" and in a different font (smaller, grey).
@sebek64
class A {
private status = false;
private subscription: Subscription | null = null;
constructor(private readonly obs: Observable<any>) {}
someMethod() {
this.subscription = this.obs.subscribe({
next: value => this.status = true,
error: err => { /* ... */ }
});
}
}
@cartant Thanks, this works. I haven't realized that this syntax is possible in Typescript. So in this case it is a good idea to keep the deprecations :)
@benlesh Do we agree this is still OK in v6.4 and further?
someObservable.subscribe(() => {});
Because when doing that, TypeScript IntelliSense is showing a signature flagged as deprecated since v6.4...
And it's not clear if the following are still OK or not:
someObservable.subscribe(() => {}, () => {});
someObservable.subscribe(() => {}, () => {}, () => {});
After some investigation, the deprecated descriptions are shown by IntelliSense in all cases!
TypeScript doesn't support to have different JSDoc for different overloads. TypeScript will aggregate all the JSDoc available for the function. It creates a very confusing situation...
Here's what you get when you hover on .subscribe() in VS Code:

I know this is a WIP but without a reasonable investigation, it looks like everything that you might pass in to subscribe has been deprecated!
I get deprecate warning on a quite valid, imo, construction. I'm on [email protected]
So, is this syntax is still allowed or not? The original PR doesn't have intention to change this.
I use WebStorm

I like the change that was done in #4202 to support this discussion.
However, it seems that now observables that I convert from other libs (e.g. mobx-utils, see simplistic https://github.com/mobxjs/mobx-utils/issues/138) don't compile.
It seems that they now must have a subscribe overload that supports all those?
Argument of type 'IObservableStream<number>' is not assignable to parameter of type 'ObservableInput<any>'.
Type 'IObservableStream<number>' is not assignable to type 'Subscribable<any>'.
Types of property 'subscribe' are incompatible.
Type '{ (observer: IStreamObserver<number>): ISubscription; (observer: (value: number) => void): ISubscription; }' is not assignable to type '{ (observer?: NextObserver<any> | ErrorObserver<any> | CompletionObserver<any> | undefined): Unsubscribable; (next: null | undefined, error: null | undefined, complete: () => void): Unsubscribable; (next: null | undefined, error: (error: any) => void, complete?: (() => void) | undefined): Unsubscribable; (next: (val...'.
Types of parameters 'observer' and 'next' are incompatible.
Type 'null | undefined' is not assignable to type 'IStreamObserver<number>'.
Type 'undefined' is not assignable to type 'IStreamObserver<number>'.