Description
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?