Skip to content

Promise.all returns all values with type of the most common interface. #16017

@megaboich

Description

@megaboich

TypeScript Version: 2.3.3 / nightly (2.4.0-dev.20170523)

The problem can be reproduced with this code:

interface A {
    id: string
}

interface B {
    id: string
    fieldB: string
}

async function countEverything(): Promise<number> {
    const providerA = async (): Promise<A[]> => { return [] }
    const providerB = async (): Promise<B[]> => { return [] }

    const [resultA, resultB] = await Promise.all([
        providerA(),
        providerB(),
    ]);

    const dataA: A[] = resultA;
    const dataB: B[] = resultB;
    if (dataA && dataB) {
        return dataA.length + dataB.length;
    }
    return 0;
}

Expected behavior:
Compiles without errors

Actual behavior:
Compilation error:

20     const dataB: B[] = resultB;
             ~~~~~
promise-all-issue.ts(20,11): error TS2322: Type 'A[]' is not assignable to type 'B[]'.
  Type 'A' is not assignable to type 'B'.
    Property 'fieldB' is missing in type 'A'.

I see that interface B is compatible with interface A, but I don't think that Promise.all should return all values as A[].
When you change interface A for example adding a field fieldA, then interface B is not compatible anymore and the code compiles without errors.

I have created very small repository which reproduces this issue.
https://github.com/megaboich/ts-promise-all-issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions