Skip to content
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

Array#map() is not callable on type-guarded array #40463

Closed
saschanaz opened this issue Sep 10, 2020 · 5 comments
Closed

Array#map() is not callable on type-guarded array #40463

saschanaz opened this issue Sep 10, 2020 · 5 comments
Assignees
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@saschanaz
Copy link
Contributor

saschanaz commented Sep 10, 2020

TypeScript Version: Version 4.1.0-dev.20200909

Search Terms: map not callable

Code

function foo<T>(o: T) {
  if (Array.isArray(o)) {
      o.map(() => {});
  }
}

Expected behavior: It should work as it does in 4.1.0-dev.20200908.

Actual behavior:

abc.ts:3:9 - error TS2349: This expression is not callable.
  Each member of the union type '(<U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: any, index: number, array: readonly any[]) => U, thisArg?: any) => U[])' has signatures, but none of those signatures are compatible with each other.

3       o.map(() => {});
          ~~~


Found 1 error.

Playground Link: It somehow shows no error on playground. https://www.typescriptlang.org/play?ts=4.1.0-insiders.20200909#code/GYVwdgxgLglg9mABMOcA8AVAfACgA4CGATgQLYBciGAlIgN4CwAUIq4jMIjgIJEkCeAOhgBnXgPzEy1WoxZsFhEqUEArODDA4ARNuoBuZgoC+zY0A

Related Issues:

@orta
Copy link
Contributor

orta commented Sep 10, 2020

This fails on this morning's nightly:

function foo<T>(o: T) {
  if (Array.isArray(o)) {
      o.map(() => {});
  }
}

Workbench Repro

I'm very willing to bet it's a side-effect of #39258

@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Sep 10, 2020
@weswigham
Copy link
Member

Because it's generic, we get the apparent type of the conditional, which means getting both the read-only array branch and non-readonly-array branch of the conditional (it could be either), which then fail to unify, as they have a separate set of generics. Essentially, we can't call .map on a union of readonly any[] and any[] because they're not subtypes of one another, and don't subtype reduce to a single set of methods.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Sep 11, 2020

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in this issue running against the nightly TypeScript.


Comment by @orta

👍 Compiled

Historical Information

Comment by @saschanaz

Version Reproduction Outputs
3.5.3, 3.6.4, 3.7.5, 3.8.3, 3.9.5, Nightly

👍 Compiled

@weswigham
Copy link
Member

@orta this should be "fixed" by the revert to the isArray type we discussed in today's meeting - wanna include it in the list of things closed by your revert PR?

@saschanaz
Copy link
Contributor Author

I think #41849 fixed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros Needs Investigation This issue needs a team member to investigate its status. Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

No branches or pull requests

6 participants