Issue
I'm working on my project with angular and typescript. here I defined an array which items can be arrays or objects.
public arrangedFooterMenu: IMenuItemType[][] | IMenuItemType[] = [];
typesOfData.forEach(type => {
let filteredData: IMenuItemType | IMenuItemType[];
filteredData = data.filter(el => {
return el.Type === type;
});
if (filteredData.length > 1) {
this.arrangedFooterMenu.push(filteredData as IMenuItemType & IMenuItemType[]);
} else {
// here in else section I get error
this.arrangedFooterMenu.push(...filteredData);
}
});
and getting error on else section's filtered data, it says. TS2345: Argument of type 'IMenuItemType' is not assignable to parameter of type 'IMenuItemType & IMenuItemType[]'.
Solution
IMenuItemType[][] | IMenuItemType[]
means only array of IMenuItemType[]s or only array of IMenuItemType.
If you want to have different type of values in same array, use (IMenuItemType | IMenuItemType[])[]
Also assign initial value to your array to be able to push items in it.
Answered By - Karen Makaryan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.