Skip to content

Union types not correctly handled in array.map #30271

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
Roaders opened this issue Mar 8, 2019 · 5 comments
Closed

Union types not correctly handled in array.map #30271

Roaders opened this issue Mar 8, 2019 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Roaders
Copy link

Roaders commented Mar 8, 2019

TypeScript Version: playground / 3..3.333

Search Terms:
union type, array map

Code

function flattenStringArrays(values: string | string[] | string[][]): string{

    if (Array.isArray(values)) {
        return values.map(value => flattenStringArrays(value)).join("");
    } else {
        return values;
    }
}

Expected behavior:
here the value in the map expression should be of type string[] | string[][]

Actual behavior:
Compilation error thrown:

Cannot invoke an expression whose type lacks a call signature. Type '((callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | ((callbackfn: (value: string[], index: number, array: string[][]) =>
U, thisArg?: any) => U[])' has no compatible call signatures.

Playground Link:
link

Related Issues:

@Roaders
Copy link
Author

Roaders commented Mar 8, 2019

This is a workaround for the issue:

function flattenStringArrays(values: string | (string | string[])[]): string{

    if (Array.isArray(values)) {
        return values.map(value => flattenStringArrays(value)).join("");
    } else {
        return values;
    }
}

link

@jack-williams
Copy link
Collaborator

This is a design limitation because it's trying to combine two generic union signatures. See these comments

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Mar 8, 2019
@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.

@gilbert
Copy link

gilbert commented Mar 25, 2019

Here's a workaround:

/** Type workaround for https://github.com/Microsoft/TypeScript/issues/7294#issuecomment-465794460 */
type ArrayElem<A> = A extends Array<infer Elem> ? Elem : never

export function elemT<T>(array: T): Array<ArrayElem<T>> {
  return array as any
}

Example usage:

type A = { x: number, y: string }
type B = { x: number, z: string }
type Subject = A[] | B[]

function foo (items: Subject) {
  // items.map(item => item.x) // Type error
  elemT(items).map(item => item.x) // Works!
}

@Robula
Copy link

Robula commented Dec 14, 2020

This is still an ongoing issue.
@DanielRosenwasser What issue does this duplicate?

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

6 participants