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

Update typing for TypeScript 1.7 "polymorphic this typing" #846

Closed
benlesh opened this issue Dec 1, 2015 · 3 comments
Closed

Update typing for TypeScript 1.7 "polymorphic this typing" #846

benlesh opened this issue Dec 1, 2015 · 3 comments

Comments

@benlesh
Copy link
Member

benlesh commented Dec 1, 2015

Thanks to @trxcllnt for pointing this out:

http://blogs.msdn.com/b/typescript/archive/2015/11/30/announcing-typescript-1-7.aspx

Basically, since our operators can return different types due to the snazzy lift architecture, we should be returning : this and not : Observable<T> from them.

... this might get a little nutty when it comes to types that change the generic output, though. map for example. Observable<T>.prototype.map((x: T) => R): Observable<R> would be Observable<T>.prototype.map((x: T) => R): this ... I'm not sure hot TypeScript will suss out that this should be generically typed to R and not T...

@kwonoj
Copy link
Member

kwonoj commented Dec 1, 2015

I was interested in this topic bit while ago when introduced 1.8 dev of typescript in RxJS, hit couple of roadblocks at the moment.

: lift returns new instance
lift does create new instance and returns it

lift(): Observable {
 ...
 return new Observable();
}

causes compiler complains about type inferences like Type 'Observable' is not assignable to type 'this'

polymorphic this requires to return self, such as

public add(operand: number) {
    this.value += operand;
    return this;
}

: polymorphic this works with instance method
most of operator declaration in Observable is based on property stubs for modularity, such as

switch: <R>() => Observable<R>

can't be converted to return this

switch: () => this;

: type transformed output
copy-paste from original comment.

Observable<T>.prototype.map((x: T) => R): Observable<R> would be
Observable<T>.prototype.map((x: T) => R): this

Still, I'm feeling this is great feature and would explore way to adopt into RxJS.

bit more details about polymorphic this can be found at https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#this-typing

@kwonoj
Copy link
Member

kwonoj commented Dec 1, 2015

Note : it is completely possible I'm not fully knowledgeable about this feature and above comment could be wrong for some aspect. Above comments are based on my initial exploration.

@benlesh
Copy link
Member Author

benlesh commented Dec 1, 2015

That's too bad. I'll close this for now, then. And perhaps file an issue with TypeScript for our specific use case/needs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants