question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pipe operator cannot infer return type as ConnectableObservable

See original GitHub issue

RxJS version: 5.5.0

Code to reproduce:

const obs = Observable.of(5);
const connectableObs = obs.pipe(
    publishReplay(1)
);

Expected behavior: The inferred type of connectableObs is ConnectableObservable<number>.

Actual behavior: The inferred type of connectableObs is Observable<number>.

Additional information:

TypeScript version: 2.5.3

My current workaround is to manually downcast the return value:

const obs = Observable.of(5);
const connectableObs = obs.pipe(
    publishReplay(1)
) as ConnectableObservable<number>;

Happy to send a PR, but not sure how should I fix this properly.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:65
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

38reactions
Dynaloncommented, Apr 25, 2018

This issue still exists in rxjs 6.0 final. Reproduce:

const connectableObservable = from([1, 2, 3]).pipe(publish());
connectableObservable.connect()

tsc will fail, saying error TS2339: Property 'connect' does not exist on type 'Observable<any>'.. Casting the connectableObservable to any and calling .connect() works as expected.

Edit: Used latest typescript 2.8.3.

19reactions
cartantcommented, Oct 19, 2017

Actually, if the overload signatures for pipe are written like this:

pipe<A, OA extends Observable<A>>(op1: UnaryFunction<Observable<T>, OA>): OA;
pipe<A, B, OB extends Observable<B>>(op1: OperatorFunction<T, A>, op2: UnaryFunction<Observable<A>, OB>): OB;
// etc.

The problem can be solved without specific reference to ConnectableObservable.

This seems to be okay with the version of TypeScript (2.0) that RxJS uses, but I’ve not investigated this thoroughly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiler cannot infer return type - swift - Stack Overflow
Swift is unable to infer the return type of a function, whose parameter is a closure, and where the return type is deduced...
Read more >
Understanding RxJS Multicast Operators | by Netanel Basal
It doesn't internally use the multicast operator itself, and as a result it always returns an observable, rather than a ConnectableObservable .
Read more >
CHANGELOG.md - ReactiveX/rxjs - Sourcegraph
takeWhile: Now returns proper types when passed a Boolean constructor. ... pipe: Ensure that unknown is inferred for 9+ arguments. (#6212) (6fa819b) ...
Read more >
rxjs@7.8.0 - jsDocs.io
A function that returns an Observable that performs rate-limiting of ... Signatures with type parameters that cannot be inferred will be ...
Read more >
Build Reactive Websites with RxJS: Master Observables and ...
Return policy: Returnable until Jan 31, 2023 ... and Subjects, followed by ways to use those types with some of the operators. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found