Skip to content

Promise.all overloads don't account for single element tuples #41831

@gigobyte

Description

@gigobyte
Contributor

TypeScript Version: 4.2.0-dev.20201205

Search Terms: Promise.all, tuple, readonly

Code

// readonly [Promise<number>]
const promises = [Promise.resolve(0)] as const

Promise.all(promises).then((results) => {
  const first = results[0]
  const second = results[1]
})

Expected behavior:

const promises = [Promise.resolve(0)] as const

Promise.all(promises).then((results) => { // results is [number]
  const first = results[0] // number
  const second = results[1] // Tuple type '[number]' of length '1' has no element at index '1'
})

Actual behavior:

const promises = [Promise.resolve(0)] as const

Promise.all(promises).then((results) => { // results is number[]
  const first = results[0] // number | undefined (if noUncheckedIndexedAccess is on)
  const second = results[1] // no errors here
})

This is because of the last overload in lib.es2015.promise.d.ts:

all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>;

Tuples with more than 1 element have expected behavior.

Playground Link: Here

Related Issues: Couldn't find any

Activity

jcalz

jcalz commented on Dec 6, 2020

@jcalz
Contributor

Related to #39788

added
RescheduledThis issue was previously scheduled to an earlier milestone
on Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.RescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @jcalz@DanielRosenwasser@andrewbranch@rbuckton@RyanCavanaugh

    Issue actions

      Promise.all overloads don't account for single element tuples · Issue #41831 · microsoft/TypeScript