Imagine you have a data structure that can either be an array of numbers or strings and you intend to iterate over this array. In TypeScript, there are various ways to handle this scenario that are all validated by the type checker.
[1, 2].map(e => console.log(e));
let arr: string[] | number[] = [1, 2];
arr.map(e => console.log(e));
However, when adding an explicit static type cast to describe the same type for arr, the compiler may throw an error:
(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.
Have you ever wondered why this error occurs, or could it potentially be a flaw in the compiler itself?