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

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

Closed
jkchao opened this issue Nov 9, 2018 · 1 comment · Fixed by #45350
Closed

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

jkchao opened this issue Nov 9, 2018 · 1 comment · Fixed by #45350
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fix Available A PR has been opened for this issue
Milestone

Comments

@jkchao
Copy link

jkchao commented Nov 9, 2018

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:

@weswigham weswigham added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Needs Investigation This issue needs a team member to investigate its status. labels Nov 9, 2018
@weswigham weswigham added this to the TypeScript 3.3 milestone Dec 8, 2018
@weswigham weswigham self-assigned this Dec 8, 2018
@RyanCavanaugh RyanCavanaugh removed this from the TypeScript 3.3 milestone Feb 1, 2019
@grassator
Copy link

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)[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
5 participants