Skip to content

.map() .reduce() .filter() methods fail with "This expression is not callable." error on valid array with union type #36390

Closed
@IvanKalinin

Description

@IvanKalinin

TypeScript Version: any

Code:

const arr: number[] | string[] = [];  // Works with Array<number | string>
const arr1: number[]  = [];
const arr2:  string[] = [];

arr.map((a: number | string, index: number) => { 
    return index
})

arr.reduce((acc: Array<string>, a: number | string, index: number) => { 
    return []
}, [])

arr.forEach((a: number | string, index: number) => { 
    return index
})

arr1.map((a: number, index: number) => { 
    return index
})

arr1.reduce((acc: number[], a: number, index: number) => { 
    return [a]
}, [])

arr1.forEach((a: number, index: number) => { 
    return index
})
arr2.map((a:  string, index: number) => { 
    return index
})

arr2.reduce((acc: string[], a: string, index: number) => { 
    return []
}, [])

arr2.forEach((a: string, index: number) => { 
    return index
})

Expected behavior: TS should let me iterate through array (I tell it the type is either array of numbers OR array of strings (not an array of mixed types). In any case the variable is an array and those methods should be callable.

Actual behavior: TS throws error "This expression is not callable."

Metadata

Metadata

Assignees

Labels

Experience EnhancementNoncontroversial enhancementsFix AvailableA PR has been opened for this issueRescheduledThis issue was previously scheduled to an earlier milestoneSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions