After writing this answer, RxJS version 6 was released, and in this version the isObservable function was added to the public API. This can be imported like this:
import { isObservable } from "rxjs";
Function Signature:
export function isObservable<T>(obj: any): obj is Observable<T>
Since it is defined using typeguard, the compiler can help you this way:
const result: any = ...; if (isObservable(result)) { result.pipe(...);
Internally, RxJS tests Observable using instanceof :
if (result instanceof Observable) { ... }
So you can use:
if (!(obs instanceof Rx.Observable)) { opts = obs || {}; obs = Rx.Observable.interval(1000); }
instanceof can be used to determine if an Observable from the RxJS library that you are using.
To determine if the Symbol.observable object Symbol.observable observable, you can find the Symbol.observable property .
If the property is present and is a function, you can get the RxJS Observable from the foreign observable by passing the value returned by calling the Symbol.observable object of the Symbol.observable property.
source share