-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Deeper inference of Promise types. #49390
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
Comments
Came across this while creating a promise that just needs to resolve on an event (don't need a value) /**
* Wait for the client to get ready.
*/
async getReady(): Promise<void> {
const promise = new Promise<void>(resolve => {
this.once('ready', () => {
resolve()
})
})
return promise
} Would save trouble if resolve types were automatically inferred |
@DarkGuy10 While this doesn't invalidate the feature request, it's worth noting that in cases like yours you can take advantage of contextual typing: /**
* Wait for the client to get ready.
*/
async getReady(): Promise<void> {
return new Promise(resolve => {
this.once('ready', () => {
resolve()
})
})
} Assigning a P.S. this method probably doesn't need to be marked |
@fatcerberus thanks for the tips! And yes, I realize now that the async is promisifying a promise. Dont know why I did that but I'll fix it! |
This issue seems related: #31146 |
Previously attempted at #40466 |
I knew there was already an issue for this, but all my searches were unsuccessful. Glad someone else found it π |
Suggestion
π Search Terms
promise required infer label:Suggestion
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
In TypeScript 4.1 the first argument of a promise's
resolve
function was made to be required rather than optional (see this blogpost). This is useful because it often catches legitimate bugs. The downside is that you always have to provide a type argument. It would be nice if TypeScript were better at inferring the types of Promises so that you don't always have to provide a type.So for instance
Would automatically result in the type
Promise<number>
.π Motivating Example
In TypeScript having to provide a type is not such a big deal. You can usually get away with something like:
But in Javascript with JSDoc, this same piece of code becomes:
which is generally a lot less clean and less readable than the TypeScript equivalent.
π» Use Cases
I frequently find myself running across code like the following:
All I want to do here is wait in the middle of an async function until a certain event has happened and then move on. I won't even be using the return value of the promise most of the time.
But because the first argument is required, TypeScript will now complain that I'm calling
r()
without any arguments. (here's a playground example)If the return type of the promise would be inferred, there' wouldn't be the need to create an extra variable and annotate the promise with the
Promise<void>
type.The text was updated successfully, but these errors were encountered: