Skip to content

Possible issue with Array.prototype.map or disjoint unions of array types #16339

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
NicholasBoll opened this issue Jun 7, 2017 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@NicholasBoll
Copy link

Array.prototype.map seems to fail if given a disjoint union of different array types

TypeScript Version: 2.2.2 (also tried 2.3.4. Reproducible on the Playground as well)

Code

function map<T>(items: T[] | string[]) {
    items.map(x => x)
}

Also fails:

function map<T>(items: Array<T> | Array<string>) {
    items.map(x => x)
}

Expected behavior:
Maybe this should work?

Actual behavior:
Type Error:

Cannot invoke an expression whose type lacks a call signature. Type '{ <U>(this: [string, string, string, string, string], callbackfn: (value: string, index: number, ...' has no compatible call signatures.

Workaround
The following does work:

function map<T extends object | string>(items: T[]) {
  items.map(x => x)
}

Perhaps not a bug since a workaround exists - I don't know if the workaround would be a preferred method?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 7, 2017

Duplicate of #7294

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 7, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants