Skip to content

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

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

Closed
maple5233 opened this issue Apr 25, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@maple5233
Copy link

maple5233 commented Apr 25, 2022

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[]
@MartinJohns
Copy link
Contributor

Duplicate of #47571. Used search terms: array map overload in:title

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Apr 25, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants