Skip to content

Incorrect return type when using [Array.prototype.map] + [callback function with overloaded signature and using shorthand] #48831

Closed
@maple5233

Description

@maple5233

Bug Report

🔎 Search Terms

is:issue array callback function overload

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ? and 4.6.2
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about overload
  • I was unable to test this on prior versions because it is the latest version

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Animal {
  name: string
}
interface Cat extends Animal {
  sound: string
}

function handleAnimal(animal: Cat): Cat
function handleAnimal(animal: Animal): Animal
function handleAnimal(animal: Cat | Animal) {
  if ('sound' in animal) {
    return animal
  }
  return animal
}

declare let cat0: Cat

// when use in Array.prototype.map with shorthand callback, it is not ok
// typeof list2 will be Animal[]
const list = [cat0].map(handleAnimal)

// when use in Array.prototype.map with not-shorthand callback, it is ok
// typeof list2 will be Cat[]
const list2 = [cat0].map((catItem) => {
  const cat = handleAnimal(catItem)
  return cat
})

🙁 Actual behavior

const list = [cat0].map(handleAnimal) // type of list:  Animal[]

🙂 Expected behavior

const list = [cat0].map(handleAnimal) // type of list:  Cat[]

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions