Issue
Using TypeScript code, I'm trying to flatten an array of objects where each element can contain children:
function getMenu (menus:Array<Menu>, name:string | undefined) {
console.log('getMenu', name, menus[0])
const test = _(menus)
.thru(function (coll) {
return _.union(coll, _.map(coll, 'children') || [])
})
console.log('getMenu 2', test)
return name
}
I have this error that is blocking me:
argument of type 'Menu[][]' is not assignable to parameter of type 'List'.
Index signatures are incompatible.
Type 'Menu[]' is missing the following properties from type 'Menu': label, icon, route, selectable, and 2 more.
NB: The code works well in JavaScript.
Solution
_.map(coll, 'children')
returns Menu[][]. We should flatten it to make it as Menu[].
Try the below code:
function getMenu(menus: Array<Menu>, name: string | undefined) {
console.log('getMenu', name, menus[0]);
const test = _(menus).thru(function(coll) {
const flattenData = flatten(_.map(coll, 'children')); // flattening the data here
return _.union(coll, flattenData);
});
return name;
}
Answered By - Yuva Raj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.