-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Need something more robust than polymorphic this
typing for RxJS
#5845
Comments
I think this is another instance of the higher order generic referenced in: #1213 combined with this types. i think you want to say something like: map<T<*> extends Observable<*>, R, U>(this: T<U>, fn: (value: U) => R): T<R> |
@mhegazy that looks something like it yeah. My C# is rusty, but I think in C# it was something like |
I'm pretty sure C# doesn't support higher-kinded types either, so it'll fail at |
No - C# does not yet have higher-kinded types. |
@weswigham bummer. Not that I think it would be too useful in C# in most cases. But since TypeScript has JavaScript as a build target, I think it would be useful here. Well, clearly, because we've hit the need. |
for your example you do not need higher kinded types, f-bounded polymorphism is sufficient and in typescript (since 1.8 I think) as |
I was pretty excited about
this
typing for RxJS, since it does have a sort of "fluent" style interface. However we have some issues with it that are discussed here: ReactiveX/rxjs#846Basically, we have operators that return same-shaped, but different generic types:
map
for example:Observable<T>.prototype.map<R>((value: T) => R): Observable<R>
The reason we were looking at
this
typing the above is thatmap
doesn't always return anObservable
, it could be called on aSubject
which is a subclass ofObservable
. In that case, it actually returns aSubject
, not anObservable
, because of our fancylift
mechanism:The Observable's lift:
https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/Observable.ts#L51-L63
The Subject's lift:
https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/Subject.ts#L37-L41
How lift is used (in
map
for example):https://github.com/ReactiveX/RxJS/blob/2896556afe8b663d4f983da1830fe407841e7d5b/src/operators/map.ts#L17
It would be fantastic if we could provide our TypeScript users with the proper type information in their IDEs. Perhaps some additional syntax like
this<R>
would fit the bill? It's also worth noting that operators always return a new instance, rather than the samethis
instance, if that makes a difference.The text was updated successfully, but these errors were encountered: