Skip to content

Array unions make method find into a field instead of method #53456

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
ThePrimeagen opened this issue Mar 23, 2023 · 3 comments
Closed

Array unions make method find into a field instead of method #53456

ThePrimeagen opened this issue Mar 23, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@ThePrimeagen
Copy link

Bug Report

types that are unions of two types of arrays, ie string[] | number[] will make the find method go from a method to a field.

if type is just string[] or if i type it as number[] or if i type it by (string | number)[] in the example below find is a method.

🔎 Search Terms

find array method

🕗 Version & Regression Information

Started seeing it today

  • Cannot compile TSC

⏯ Playground Link

TSPlayground Example

💻 Code

function wut(events: string[] | number[]) {
  events.find((e) => typeof e === "string" ? e === "foo" : e === 69);
  // --------^  find is not a method
}

🙁 Actual behavior

TSC has hurt feelings (build fails)

🙂 Expected behavior

TSC says this is great (just like map, or other array methods)

@MartinJohns
Copy link
Contributor

Duplicate of #44373.

@fatcerberus
Copy link

fatcerberus commented Mar 23, 2023

types that are unions of two types of arrays, ie string[] | number[] will make the find method go from a method to a field.

For the record, that’s not what’s happening here at all.

Each member of the union type … has signatures, but none of those signatures are compatible with each other.

Basically, TS doesn’t know whether to use the find from string[] or the one from number[], and isn’t able to unify them because they have conflicting requirements.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 23, 2023
@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

5 participants