Deeper inference of Promise types.
See original GitHub issueSuggestion
š Search Terms
promise required infer label:Suggestion
ā Viability Checklist
My suggestion meets these guidelines:
- This wouldnāt be a breaking change in existing TypeScript/JavaScript code
- This wouldnāt change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isnāt a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScriptās Design Goals.
ā 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
new Promise(r => r(3));
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:
await new Promise<number>(r => r(3));
But in Javascript with JSDoc, this same piece of code becomes:
/** @type {Promise<number>} */
const promise = new Promise(r => r(3);
await promise;
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:
/** @type {(() => void)[]} */
const cbs = [];
async function test() {
// Do something
// wait until `cbs` have fired
await new Promise(r => r());
// Do something else
}
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.
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:6 (1 by maintainers)
Top GitHub Comments
@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:
Assigning a
new Promise()
to an existingPromise
type automatically infers the correct type forresolve
.P.S. this method probably doesnāt need to be marked
async
since it returns a promise directly and never usesawait
. You generally want to avoid that because it imposes an extra layer of promise-wrapping and by extension, extra event loop ticks.Came across this while creating a promise that just needs to resolve on an event (donāt need a value)
Would save trouble if resolve types were automatically inferred