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
@UselessPickles

Description

@UselessPickles

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

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