microsoft / microsoft/TypeScript

Promise constructor's return type doesn't unwrap nested promises

Aperta
#52,764 1 commento 3 reazioni 1 assegnatario Vedi su GitHub

@rbuckton ci sta già lavorando.

Dal 16/2/2023.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.