Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
UselessPickles opened this issue Mar 4, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@UselessPickles
Copy link

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

@UselessPickles
Copy link
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?

@UselessPickles UselessPickles 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. Mar 4, 2020
@nmain
Copy link

nmain commented Mar 4, 2020

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.

@UselessPickles
Copy link
Author

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 4, 2020
@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants