TypeScript: Promise.all not correctly inferring types.

TypeScript Version: 3.7.2, v3.8.0-dev.20191102

Search Terms: Promise.all Wrong type

Code

const f1 = async (): Promise<string> => {
    return '';
};

const f2 = async (): Promise<number> => {
    return 0;
};

const f3 = async (): Promise<Object> => {
    return {};
};

const res = Promise.all([f1(), f2(), f3()]);

Expected behavior: res has type Promise<[string, number, Object]>.

Actual behavior: res has type Promise<Object[]>.

Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20191102&ssl=2&ssc=1&pln=5&pc=1#code/MYewdgzgLgBAZgRhgXhgQwgTzMGAKASgC4YAFAJxAFsBLCAUwB5pyawBzAPhW4G8AoGEJjl6UAK7kwMAOQyA3PwC+-fqEiw4AJhTosOfMTKVaDRmHFUARvXLdkfQcNESpMAAyKVa8NHgBmXQxsXEISCmo6JgB5KwAremAoe0dhETFJaV4lL1V1P1EIXQjTegA6NAAbSrwAbURCABp4LSaAwgBdAnkgA

Related Issues:

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 80
  • Comments: 16 (6 by maintainers)

Most upvoted comments

Hey, @RyanCavanaugh!

Is there any chance a fix of this or related (#34937, #35258, etc) issues could be squeezed into some of the next 3.7.x releases? Seems like Promise.all typings are broken in lots of projects in 3.7.3.

I’ve got another example here. From my tests, it looks like this occurs whenever one of the promises has a narrower type that is assignable to the type of another promise in the list. I’ve only been able to get around it by explicitly typing Promise.all.

Note that I’m seeing this type widening occur on earlier versions of TS than 3.7, at least in the playground.

Copying code from the playground here too:

type Thing1 = Record<string, string>;
type Thing2 = {a: string; b: string;}; // assignable to Thing1

const main = async() => {

    const thing1: Thing1 = {hello: 'world'}; // const thing1: Record<string, string>
    const thing2: Thing2 = {a: 'hello', b: 'world'}; // const thing2: Thing2

    const things = [thing1, thing2];  // const things: Record<string, string>[]

    const do1 = async (t1: Thing1) => t1;
    const do2 = async (t2: Thing2) => t2;

    const promise1 = do1(thing1);  // const promise1: Promise<Record<string, string>>
    const promise2 = do2(thing2);  // const promise2: Promise<Thing2>

    const promises = [promise1, promise2];  // const promises: Promise<Record<string, string>>[]

    // const superPromiseFromPromises: Promise<Record<string, string>[]>
    const superPromiseFromPromises = Promise.all(promises);
    
    // const a1: Record<string, string>
    // const a2: Record<string, string>
    const [a1, a2] = await superPromiseFromPromises;

    // const superPromiseFromLiteral: Promise<Record<string, string>[]>
    const superPromiseFromLiteral = Promise.all([promise1, promise2]);

    // const b1: Record<string, string>
    // const b2: Record<string, string>
    const [b1, b2] = await superPromiseFromLiteral;

    // const c1: Record<string, string>
    // const c2: Record<string, string>
    const [c1, c2] = await Promise.all([promise1, promise2]);

    // const explicitlyTypedPromises: [Promise<Record<string, string>>, Promise<Thing2>]
    const explicitlyTypedPromises: [Promise<Thing1>, Promise<Thing2>] = [promise1, promise2];

    // const d1: Record<string, string>
    // const d2: Record<string, string>
    const [d1, d2] = await Promise.all(explicitlyTypedPromises);

    // const e1: Record<string, string>
    // const e2: Thing2
    const [e1, e2] = await Promise.all<Thing1, Thing2>([promise1, promise2]);
}

@stevehollaar

#33707 will fix that example.

type Awaited<T> =
    T extends undefined ?
    T :
    T extends PromiseLike<infer U> ?
    U :
    T
;
declare function all<T extends readonly any[]>(
    values: T
): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;

//for empty tuple
declare function betterAll (arr : []) : Promise<[]>;
//for non-empty tuple
declare function betterAll<ArrT extends readonly [any, ...any[]]>(
    arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;
//for non-tuple array
declare function betterAll<ArrT extends readonly any[]>(
    arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;

//https://github.com/microsoft/TypeScript/issues/34925#issuecomment-550021453
const fetch1 = (): Promise<number | null> => Promise.resolve(1);
const fetch2 = (): Promise<string[]> => Promise.resolve(['a', 'b']);

async () => {
  //Error
  const [r1, r2]: [number|null, string[]] = await Promise.all([fetch1(), fetch2()]);
  console.log(r1,r2)
}

async () => {
  //OK
  const [r1, r2]: [number|null, string[]] = await all([fetch1(), fetch2()]);
  console.log(r1,r2)
}

async () => {
  //OK
  const [r1, r2]: [number|null, string[]] = await betterAll([fetch1(), fetch2()]);
  console.log(r1,r2)
}

async () => {
  //OK
  //const r1: number | null
  //const r2: string[]
  const [r1, r2] = await all([fetch1(), fetch2()]);
  console.log(r1,r2)
}

async () => {
  //OK
  //const r1: number | null
  //const r2: string[]
  const [r1, r2] = await betterAll([fetch1(), fetch2()]);
  console.log(r1,r2)
}

Playground

I have also found that using Promise.all in such way:

const [pr1, pr2] = await Promise.all([fn1(), fn2()]);

resolves pr1, pr2 variable types to type | null if any of the async functions which are present in Promise.all can alternatively return null.

@thaoula

type Awaited<T> =
    T extends undefined ?
    T :
    T extends PromiseLike<infer U> ?
    U :
    T
;
declare function all<T extends readonly any[]>(
    values: T
): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;

//for empty tuple
declare function betterAll (arr : []) : Promise<[]>;
//for non-empty tuple
declare function betterAll<ArrT extends readonly [any, ...any[]]>(
    arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;
//for non-tuple array
declare function betterAll<ArrT extends readonly any[]>(
    arr: ArrT
): Promise<{ -readonly [i in keyof ArrT]: Awaited<ArrT[i]> }>;

//https://github.com/microsoft/TypeScript/issues/34925#issuecomment-550092250
//const res0: Promise<any[]>
const res0 = Promise.all([Promise.resolve(1), Promise.resolve(null as any)]);
//const res1: Promise<any[]>
const res1 = all([Promise.resolve(1), Promise.resolve(null as any)]);
//const res2: Promise<[number, any]>
const res2 = betterAll([Promise.resolve(1), Promise.resolve(null as any)]);

Playground

We have quite a few Promise.all and array destructuring statements in our codebase. Most are working like the Typescript 3.6.3 version except any statement that involves promises returning any.

Whenever any promise in the Promise.all call returns any all the destructured variables are marked as any.

Is there any chance a fix of this or related (#34937, #35258, etc) issues could be squeezed into some of the next 3.7.x releases?

@RyanCavanaugh @rbuckton friendly ping, just in case you missed my message