Closed
Description
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."