microsoft / microsoft/TypeScript
Promise constructor's return type doesn't unwrap nested promises
Offen
@rbuckton arbeitet bereits daran.
Seit 16.2.2023.
Needs Investigation
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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?
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.