microsoft / microsoft/TypeScript
Promise constructor's return type doesn't unwrap nested promises
Open
@rbuckton is already working on this.
Since Feb 16, 2023.
Needs Investigation
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
promise constructor
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about Promises
⏯ Playground Link
Playground link with relevant code
💻 Code
const nestedPromise = (function() {
return new Promise<Promise<number>>(resolve1 => {
resolve1(new Promise<number>(resolve2 => {
resolve2(5);
}));
})
})();
// according to TS, `result` should be a Promise<number>, but at runtime, it's actually just a number
nestedPromise.then(result => console.log('first', result, typeof result));
// ^?
// when using Promise.resolve(), the type is correct
const nestedPromiseResolve = (function() { return Promise.resolve(Promise.resolve(Promise.resolve(5)))})();
nestedPromiseResolve.then(result => console.log('second', result, typeof result));
// ^?
🙁 Actual behavior
Return type of the promise constructor doesn't unwrap nested promises
🙂 Expected behavior
The return type of the promise constructor should match the runtime behavior
The current constructor type is { new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>; }
Shouldn't the return type actually be Promise<Awaited<T>> like it is for Promise.resolve()? Is there something I'm missing here?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.