Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ajax() returns Observable<{}> instead of Observable<AjaxResponse> #1783

Closed
billba opened this issue Jun 24, 2016 · 8 comments · Fixed by #1813
Closed

ajax() returns Observable<{}> instead of Observable<AjaxResponse> #1783

billba opened this issue Jun 24, 2016 · 8 comments · Fixed by #1813
Assignees
Labels
TS Issues and PRs related purely to TypeScript issues

Comments

@billba
Copy link

billba commented Jun 24, 2016

RxJS version: 5.0.0-beta.9

Code to reproduce:

const source = Observable.ajax({ method:"GET", url: "myurl" });

Expected behavior:

Mouse over source to see that it's type Observable<AjaxResponse>

Actual behavior:

It's type Observable<{}>.

Additional information:

What might be going on is that AjaxObservable<T>.create has

      return new AjaxObservable(urlOrRequest);

and I think it should be:

      return new AjaxObservable<AjaxResponse>(urlOrRequest);

As a workaround I was able to do:

const source = Observable.ajax<AjaxResponse>({ method:"GET", url: "myurl" });

... but ajax shouldn't really take a type at all and I'm not sure why it even can.

@kwonoj
Copy link
Member

kwonoj commented Jun 24, 2016

Seems it makes sense. Let me look bit more and create PR if necessary.

@kwonoj kwonoj self-assigned this Jun 24, 2016
@kwonoj kwonoj added the TS Issues and PRs related purely to TypeScript issues label Jun 24, 2016
@benlesh
Copy link
Member

benlesh commented Jun 27, 2016

cc/ @david-driscoll

@kwonoj
Copy link
Member

kwonoj commented Jun 28, 2016

I've looked this into bit more to see if it's possible to get rid of generics for ajax creation method, found it's bit tricky.

but ajax shouldn't really take a type at all and I'm not sure why it even can.

: Main reason for this is AjaxRequest allows resultSelector makes AjaxObservable to emit other than AjaxResponse, Observable.ajax<AjaxResponse>({ method:"GET", url: "myurl" }) is actually valid way to specify types.

While it's expected to allow generics to AjaxObservable I'm also feeling type definitions can be improved bit better to work with resultSelector as well as default, not sure about immediate changes though. I'll leave this open for couple of more days to get idea around.

@billba
Copy link
Author

billba commented Jun 28, 2016

Ah but here's the thing -- ajax, which is to say AjaxObservable.create -- isn't written as a generic function. That's why it's so weird that you can even give it a type. I think what's happening is that TypeScript "needs" a type for that AjaxObservable constructor, and so it lets you add one farther up the stack. Which sure sounds like a bug in TypeScript, but that's a different issue for a different rep.

On the larger subject, my two cents is this: let ajax continue to return the full AjaxResponse, just like in jQuery. It shouldn't be a generic.

If you want to access a REST JSON response as a type, you don't need to resort to resultSelector. You can just do:

const source = Observable.ajax({ method:"GET", url: "myurl" }).map(ajaxResponse => ajaxResponse.response as TheTypeOfTheRestResponse);

Ditto if you just want part of the response:

const source = Observable.ajax({ method:"GET", url: "myurl" }).map(ajaxResponse => ajaxResponse.response["foo"]);

Both are simpler and clearer (and more Rx-y) than the equivalent resultSelector code, no?

tl;dr: let ajax be ajax, death to resultSelector

@david-driscoll
Copy link
Member

I don't personally see a ton of value in the result selector (after looking at the code) which would make this problem very easy to solve.

I'll leave it up to @Blesh and @kwonoj though.

@kwonoj
Copy link
Member

kwonoj commented Jun 28, 2016

I'm also leaning towards to drop off resultSelector, just unsure if there's solid usecases for those. (But as @billba mentioned, most cases map can resolve selections..)

kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 8, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
@kwonoj
Copy link
Member

kwonoj commented Jul 8, 2016

I've filed PR #1813 to leverage as discussion point. Basically PR drops resultSelector and ensure type delivered to subscription, encourage to use map where selection needed.

kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 9, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 10, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 10, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 10, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 11, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jul 17, 2016
closes ReactiveX#1783

BREAKING CHANGE: ajax.*() method no longer support resultSelector, encourage to use `map` instead
@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
TS Issues and PRs related purely to TypeScript issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants