If I have array of union I can check for typeof value this way:
//case 1
function something1(a1: Array<number | string | boolean>)
{
for (const v of a1)
if (typeof v === "number")
v; //v is number
else if (typeof v === "boolean")
v; //v is boolean
else
v; //v is string
}
If I have union of array I could do it the same way:
//case 2
function something2(a1: Array<number> | Array<string> | Array<boolean>)
{
for (const v of a1)
if (typeof v === "number")
v; //v is number
else if (typeof v === "boolean")
v; //v is boolean
else
v; //v is string
}
but I want to avoid checking for type inside loop:
//case 3
function something3(a1: Array<number> | Array<string> | Array<boolean>)
{
if (a1.length === 0)
return;
if (typeof a1[0] === "number")
a1; //should be number[]!! but it is number[] | string[] | boolean[]
if (typeof a1[0] === "boolean")
a1; //should be boolean[]!! but it is number[] | string[] | boolean[]
if (typeof a1[0] === "string")
a1; //should be string[]!! but it is number[] | string[] | boolean[]
}
but a1 is not recognised as number[] or string[] or boolean[].
But this makes sense. Since all items in array are the same type. Is it possible to achieve that?
I am using TypeScript beta 2.0.