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

WebSocketSubjectConfig's resultSelector is excepting an not necessary generic type T #2754

Closed
fangpenlin opened this issue Jul 18, 2017 · 5 comments
Labels
TS Issues and PRs related purely to TypeScript issues

Comments

@fangpenlin
Copy link

RxJS version:

5.4.2

Code to reproduce:

Say if you want to make WebSocket subject emits raw MessageEvent, and you write

import * as Rx from 'rxjs/Rx'

Rx.Observable.webSocket<MessageEvent>({
  url: 'ws://localhost:8080',
  resultSelector: (e: MessageEvent) => e
})

Expected behavior:

It should compiles

Actual behavior:

[ts]
Argument of type '{ url: string; resultSelector: (e: MessageEvent) => MessageEvent; }' is not assignable to parameter of type 'string | WebSocketSubjectConfig'.
  Type '{ url: string; resultSelector: (e: MessageEvent) => MessageEvent; }' is not assignable to type 'WebSocketSubjectConfig'.
    Types of property 'resultSelector' are incompatible.
      Type '(e: MessageEvent) => MessageEvent' is not assignable to type '<T>(e: MessageEvent) => T'.
        Type 'MessageEvent' is not assignable to type 'T'.
(property) resultSelector: (e: MessageEvent) => MessageEvent

Additional information:

I just upgraded to typescript 2.4.1, maybe it has something to do with this change

I can work around the issue either by returning any type from the resultSelector function, or make it a generic function like this

import * as Rx from 'rxjs/Rx'

function resultSelector<T> (e: MessageEvent): T {
  return e as any as T
}

Rx.Observable.webSocket<MessageEvent>({
  url: 'ws://localhost:8080',
  resultSelector
})

But returning any is losing the type check, as we know exactly what type to be expected as it's given as an Rx.Observable.webSocket type argument. And for the second working around, which is even more awkward .

So, maybe a more ideal solution is to make WebSocketSubjectConfig also a generic type, and use the T as the return value from resultSelector?

@kwonoj
Copy link
Member

kwonoj commented Jul 18, 2017

Yes, it seems legit and this seems oversight when we didn't have strict generic checks.

@kwonoj kwonoj added the TS Issues and PRs related purely to TypeScript issues label Jul 18, 2017
@fangpenlin
Copy link
Author

@kwonoj created a bugfix at #2755

@jrmcdona
Copy link

jrmcdona commented Dec 9, 2017

I am still facing issues with this.

Here is the code for the WebSocketSubjectConfig.

   this.wsSubjectConfig = {
      url: url,
      closeObserver: {
        next: (e: CloseEvent) => {
          this.socket = null;
          this.connectionObserver.next(false);
        }
      },
      openObserver: {
        next: (e: Event) => {
          this.connectionObserver.next(true);
        }
      },
      resultSelector: (e: MessageEvent) => {
        return this.serialization.handle(e);
      }
    };

here is the error:

src/core/events/rxWebSocket.subject.ts(37,5): error TS2322: Type '{ url: string; closeObserver: { next: (e: CloseEvent) => void; }; openObserver: { next: (e: Event...' is not assignable to type 'WebSocketSubjectConfig'.
  Types of property 'resultSelector' are incompatible.
    Type '(e: MessageEvent) => DataEvent' is not assignable to type '<T>(e: MessageEvent) => T'.
      Type 'DataEvent' is not assignable to type 'T'.

Notice anything I should be doing different?

@benlesh
Copy link
Member

benlesh commented Aug 18, 2020

Closing this, as resultSelector has been deprecated for some time.

@benlesh benlesh closed this as completed Aug 18, 2020
@mfit
Copy link

mfit commented Feb 27, 2022

@benlesh but how can the OP's use case be fulfilled (not apply JSON.parseon the incoming messages) ?

Only now I figured out that there are serializer / deserializer arguments in the config of the subject ( https://rxjs.dev/api/webSocket/WebSocketSubjectConfig)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TS Issues and PRs related purely to TypeScript issues
Projects
None yet
Development

No branches or pull requests

5 participants