Here are two array variables with the following structure:
export interface IShop {
name: string,
id: number,
type: string,
}
export interface IHotel {
name: string,
id: number,
rooms: number,
}
The TypeScript code is as shown below:
let stores: IShop[];
let accommodations: IHotel[];
//these variables then gets assigned respective data from an API matching the interfaces
const allAreas = stores.length > 0 ? stores : (accommodations.length > 0 ? accommodations : []);
allAreas.find(a => a.name === 'name');
On the last line, an error occurs stating:
Cannot invoke an expression whose type lacks a call signature. Type '{ (predicate: (this: void, value: IShop, index: number, obj: IShop[]) => value is S, thisArg?: any): S; (predicate: (value: IShop, index: number, obj: IShop[]) => boolean, thisArg?: any): IShop; } | { ...; }' has no compatible call signatures.
A similar issue arises for other Array methods during compilation, though the code functions correctly and the meaning of the problem is understood. However, why isn't TypeScript recognizing the Array?
When checking the type of allRegions
, it returns IShop[] | IHotel[]
, both of which are clearly arrays. Is there a potential datatype issue with allRegions
?