I have an array foo of the type:
foo: ObjectId[] | string[];
I need to assign it to a string array bar as follows :
let bar : string[] = foo.map( (e) => e.toString())
This should be correct since both ObjectID and string types have toString() method defined. However the above code is not compiled because of the following error :
This expression is not callable.
Each member of the union type '(<U>(callbackfn: (value: ObjectId, index: number, array: ObjectId[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[])' has signatures, but none of those signatures are compatible with each other.
I am currently using the following workaround :
let bar : string[] = (foo as Array<string|ObjectID>).map( (e) => e.toString())
Is there some better way to cast the array of mixed type to string?