Skip to content

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

Closed
@NicholasBoll

Description

@NicholasBoll

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?

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