-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
.await has wrong type? #457
Comments
@jasonkuhrt I'm not really sure a correct type definition can be written for the method form of I don't know why TS would infer a type of |
Ah I just saw your comment about awaitPromises being missing from the TS defs, too! Good catch. We should def fix that as well. |
Oh but function form yes?
What would higher-kinded types look like in this case? FlowType has the same limitation? |
No idea if this would help: https://medium.com/@gcanti/higher-kinded-types-in-typescript-static-and-fantasy-land-d41c361d0dbe |
Yep, it's possible for the function form. The type def shows how
In this case, there would need to be a way to express that the
It does indeed. I'm not aware of any way to do it Flow, either. @gcanti has done some pretty amazing work in simulating HKTs and more recently, representing them using property types (I think that's what they're called) in TS. It seems like applying his patterns may require a fairly significant restructuring of most.js, but I really have no idea right now. |
@briancavalier I've never used |
Maybe this can be helpful declare class Stream<A> {
map<B>(f: (a: A) => B): Stream<B>
// you can define a type annotation for this
await<B>(this: Stream<Promise<B>>): Stream<B>
// etc...
}
new Stream<number>().await() // error
new Stream<Promise<number>>().await() // ok
new Stream<string>().map(s => Promise.resolve(s.length)).await() // ok |
Thanks, @gcanti. I didn't know type constraints on Looks like there's at least some discussion about adding an equivalent feature to Flow. |
Yeah, in 1.x it isn't. In |
@briancavalier seems a pretty old feature (v2.0.0) https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#specifying-the-type-of-this-for-functions |
@gcanti Great, seems safe to require it then. Thanks for the help 👍 @jasonkuhrt If you're still up for a PR, I'd certainly welcome one that applies a |
@briancavalier I'll give it a go! I'll try to do it this week. |
@jasonkuhrt this just landed in most 1.6.1. Thanks again for raising the issue, and thanks @gcanti for the solution. |
Summary
I think the TypeScript type for
Most.await
is problematic. Consider this code:Expected result
It should return a
Stream<Change>
.Actual Result
It instead returns a
Stream<{}>
. This is because ofawait
which has a return value ofStream<{}>
.Versions
^1.4.0
I will try to come back with a PR or failing that a simplified repro.
The text was updated successfully, but these errors were encountered: