Skip to content

Promise.all return type incorrect when some promise types are unions of Promise types, rather than Promise of union of types. #37216

Closed

Description

TypeScript Version: 3.8.3 (regression - does not occur in 3.7)

Search Terms: Promise.all return type

Code

declare const conditional: boolean;

async function foo(): Promise<void> {
    // Note: promiseA cannot possibly resolve to undefined
    const promiseA = Promise.resolve("hello");
    const promiseB = conditional ? Promise.resolve(10) : Promise.resolve(undefined);

    const [resultA, resultB] = await Promise.all([promiseA, promiseB]);

    // Unexpected error here: resultA is possibly undefined
    alert(resultA.toUpperCase());
}```

**Expected behavior:**
Type of `resultA` should be `string`;

**Actual behavior:**
Type of `resultA` is `string | undefined`;

**Workaround:**
Tack on a `as const` to the tuple of promises being passed to `Promise.all()`:
```ts
    const [resultA, resultB] = await Promise.all([promiseA, promiseB] as const);

Playground Link: http://www.typescriptlang.org/play/index.html#code/CYUwxgNghgTiAEYD2A7AzgF0a4BLDuqUEAXPAEZJIQhQoDcAUI1GgJ4pjwBmArpwVQ8qACgCUZAAowkAW1xoQAHgBuSXMAB88AN6N4B+AHoj8AHJIMIMgAcZ8xQEFEdFJfg2kaNLnIQ28HBo1CoIGEjw-KDcuCggwPqGyOhYdnIKIM4AvPDS6YoAdEEhICIARAAWIBAQSGViTIbYKR72GQBC8DnJeIIoxPAA-LlthcUQoSIAjAAMYvBSoyBFIMETpVEgMXHADcxNyZjwANpBvBAYjgA0gavnGO0Aul3wUADuUPgj+cvEECLHNIOTI3IEdR57RIGEzwACqcQAHjZwFZgPAQDAZDB4FU4GQzhdnAoPF4fH4ApttvEoa8aDAMCICZcCuFYTZkTAAMKsUpiPYAX2YQA

Activity

  1. UselessPickles commented on Mar 4, 2020

    @UselessPickles
    Author

    I think I just realized that undefined has nothing to do with this, but instead that one of the promises' type is a union of different Promise types?

  2. changed the title [-]Promise.all return type incorrect when some promises may resolve to undefined[/-] [+]Promise.all return type incorrect when some promise types are unions of Promise types, rather than Promise of union of types.[/+] on Mar 4, 2020
  3. nmain commented on Mar 4, 2020

    @nmain

    Search Terms: Promise.all return type

    Probable duplicate of #35626
    Probable duplicate of #34937
    Probable duplicate of #33562

    I think these all trace back to #33057, which is my fault, so I am sorry.

  4. UselessPickles commented on Mar 4, 2020

    @UselessPickles
    Author

    Nicholai Main (@nmain) Thanks. I don't know what's wrong with my eyes. Looking at the search results again it seems obvious that those were likely duplicates I should have looked at.

  5. typescript-bot commented on Mar 7, 2020

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions