Skip to content

when has optional elements in tuple types, Promise.all has error #28427

Closed

Description

@jkchao

TypeScript Version: 3.1.3

Search Terms:
Promise.all, optional elements in tuple types

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.

const arr: [Promise<number>, Promise<string>?] = [
  Promise.resolve(1)
];

arr[1] = Promise.resolve('1');

Promise.all(arr);    // Error

// or

const arr2: Array<Promise<number> | Promise<string>> = [Promise.resolve(1)];
arr2.push(Promise.resolve('1'));

Promise.all(arr2);   // Error

Expected behavior:

Promise.all(arr) should be correct

Actual behavior:

Activity

  1. added
    BugA bug in TypeScript
    Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Nov 9, 2018
  2. grassator commented on Feb 15, 2019

    @grassator

    The same (or similar) error happens when working with an array that can contain undefined items, which is OK according to the spec:

    declare const foo: (Promise<number> | undefined)[]
    Promise.all(foo) // error

    Since TS now has conditional types, I could imagine that the definition should be something like:

    type UnwrapPromise<T> = T extends PromiseLike<infer U> ? U : T
    
    declare function promiseAll<T>(values: T[]) : UnwrapPromise<T>;
    promiseAll(foo) // (number | undefined)[]
  3. added a commit that references this issue on Aug 23, 2019
    1d79d5d
  4. 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

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions