镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Union types not correctly handled in array.map #30271

Description

@Roaders

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:

Activity

  1. Roaders commented on Mar 8, 2019

    @Roaders
    Author

    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

  2. jack-williams commented on Mar 8, 2019

    @jack-williams
    Collaborator

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

  3. typescript-bot commented on Mar 11, 2019

    @typescript-bot
    Contributor

    This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  4. gilbert commented on Mar 25, 2019

    @gilbert

    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!
    }
  5. Robula commented on Dec 14, 2020

    @Robula

    This is still an ongoing issue.
    Daniel Rosenwasser (@DanielRosenwasser) What issue does this duplicate?

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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