Say that you have something which can either be an array of numbers or strings and you want to map over this array. Using TypeScript the following ways of expressing such a situation are all accepted by the type checker.
[1, 2].map(e => console.log(e));
let arr: string[] | number[] = [1, 2];
arr.map(e => console.log(e));
But if we add an explicit static type cast, to the same type, to describe arr the compiler will throw an error our way:
(arr as string[] | number[]).map(e => console.log(e));
// Cannot invoke an expression whose type lacks a call signature.
// Type '(<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (...' has no compatible call signatures.
Do you know why this would happen, or is it possible that this is an issue with the compiler itself?
(arr as string[] & number[]).map(e => console.log(e));. Either that, or use a type guard.