There's something peculiar going on with TypeScript. I have a type union that includes array types (string[]
, number[]
) and non-array types (string
, number
). When using type inference, everything behaves as expected:
type bar = string | number | string[] | number[];
declare foo: bar;
if (Array.isArray(foo))
{
foo // string[] | number[]
}
else
{
foo // string | number
}
However, when attempting to constrain the type directly to array types using a type intersection, the outcome is unexpected:
declare foo: bar & any[];
// expected type: string[] | number[]
foo // (string & any[]) | (number & any[]) | (string[] & any[]) | (number[] & any[])
Why does this happen?
Shouldn't string & any[]
result in never
and string[] & any[]
in string[]
?