Skip to content

TS 4.2 Promise.all can't deal with array + spread #43157

Closed
@bfricka

Description

@bfricka

Bug Report

🔎 Search Terms

  • no overload matches call created:>=2021-01-01
  • promise created:>=2021-01-01
  • iterable created:>=2021-01-01

🕗 Version & Regression Information

  • 4.2.2
  • 4.2.3
  • Still reproduces in nightly 4.3.0-dev.20210308
  • Works fine in 4.1 and below
  • This changed between versions 4.1.5 and 4.2

⏯ Playground Link

Playground link with relevant code

💻 Code

const arrFoo = async () => ['foo']
const foo = async () => 'foo'
const bar = async () => 'bar'

const test = async () => {
    const mapToBarPromises = [1, 2, 3].map(bar)
    
    const onlyBars = await Promise.all(mapToBarPromises)
    const onlyBarsSpread = await Promise.all([...mapToBarPromises])
    const mixed = await Promise.all([foo(), bar()])
    const mixedWithArr = await Promise.all([arrFoo(), bar()])
    const mixedWithSpread = await Promise.all([foo(), ...mapToBarPromises])
    // Fail when mixing spread with other arrays
    const mixedArrAndSpread = await Promise.all([arrFoo(), ...mapToBarPromises])
}

🙁 Actual behavior

TypeScript 4.2 Promise.all can't deal with a Promise<T1[]> plus a spread Promise<T2>

🙂 Expected behavior

This shouldn't be an error and in previous versions, it wasn't. Since it's very common for something async to return a list it makes it painful to do something like:

const [teams, ...players] = await Promise.all([getTeams(), ...regions.map(getPlayers)])

Sorry, I couldn't think of a less contrived example. More likely in code I see, the spread is a list of side effects updates, where we don't actually care about the result:

await Promise.all([
  updateUsers(),
  ...teams.map(updateTeam),
])

I can do Promise.all<any>(), but if we actually want the values, we have to do some elaborate typing that we shouldn't have to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions