-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Describe the bug
function handleObservableOrNext<T>(
observable: Observable<T>,
observerOrNext?: Partial<Observer<T>> | ((value: T) => void)
) {
observable.subscribe(observerOrNext);
// type error here
}
Expected behavior
no type error
Reproduction code
As described above
Reproduction URL
https://stackblitz.com/edit/typescript-vrgcva
Version
latest
Environment
No response
Additional context
-
Should the
next
param here be optional?
rxjs/src/internal/Observable.ts
Line 78 in 081ca2b
subscribe(next: (value: T) => void): Subscription;
I think this param should be optional, since the implements of the overload support optional
rxjs/src/internal/Observable.ts
Line 214 in 081ca2b
observerOrNext?: Partial<Observer<T>> | ((value: T) => void) | null, -
Are the overloads here good overloads?
According to Writing Good Overloads in typescript handbook, with the same argument count and same return type, I don't think 2 different overloads here is good overloads.
Always prefer parameters with union types instead of overloads when possible.
rxjs/src/internal/Observable.ts
Lines 77 to 78 in 081ca2b
subscribe(observer?: Partial<Observer<T>>): Subscription; | |
subscribe(next: (value: T) => void): Subscription; |
could be merged into one overload function like
subscribe(observer?: Partial<Observer<T>> | ((value: T) => void)): Subscription;
ref: https://www.typescriptlang.org/docs/handbook/2/functions.html#writing-good-overloads