I am working on next.js 15 project using typescript. The goal is to create nested Navigation.
When I run npm run dev it compiles correctly but when I try to build the project I am getting the following error:
Type error: Type 'menuListProps | undefined' is not assignable to type 'menuItemProps[]'.
Type 'undefined' is not assignable to type 'menuItemProps[]'.
90 | <li key={id}>
91 | <h2>{title}</h2>
> 92 | <MenuContent subMenuItems={subMenuItems} />
| ^
93 | </li>
94 | </>
95 | )
data for menu:
const menuItems = [
{
id:getRandomInt(maxIntegerRandom),
title:"a",
subMenuItems: [
{
id:getRandomInt(maxIntegerRandom),
title:"a1",
subMenuItems: [
{
id:getRandomInt(maxIntegerRandom),
title:"a11",
},
{
id:getRandomInt(maxIntegerRandom),
title:"a12",
},
],
},
{
id:getRandomInt(maxIntegerRandom),
title:"a2",
subMenuItems: [
{
id:getRandomInt(maxIntegerRandom),
title:"a21",
},
],
},
]
}
]
types definitions:
type menuListProps = {
passedMenuItems: menuItemProps[];
}
type menuItemProps = {
id: number;
title: string;
shortTitle?: string;
description?: string;
shortDescription?: string;
path?: string;
image?: string;
icon?: string;
hideForHamburgerMenu?: boolean;
subMenuItems?: menuListProps;
}
components:
function MenuContent(props: menuListProps){
const { passedMenuItems } = props;
let toRender = null;
toRender = passedMenuItems.map( (menuItem) => MenuItem(menuItem) )
return (
<>
<ul>
{toRender}
</ul>
</>
)
}
function MenuItem (props: menuItemProps){
const { id, path, title, subMenuItems, hideForHamburgerMenu } = props;
if (!("hideForHamburgerMenu" in props && hideForHamburgerMenu))
{
if ("subMenuItems" in props)
return (
<>
<li key={id}>
<h2>{title}</h2>
<MenuContent passedMenuItems={subMenuItems} />
</li>
</>
)
else
return (
<li key={id}>
<h2>{title}</h2>
</li>
)
}
}
default function:
export default function MobileAsideMainMenu(){
return(
<>
<MenuContent passedMenuItems={menuItems} />
</>
)
}
Please help me investigate what am I doing wrong?
2
Answers
Thank you for your effort. I have simplified my code and now it has started working. The following solution compiles and builds correctly, but I struggled with it until the last minute. Only after I used
and wrap into menuItem inside the type definition it start compiling correctly. Do you see any better options on how to write it better or redesign the menuItem data framework?
Your type
menuItemProps
defines the property assubMenuItems?: menuListProps
which means two things:undefined | menuListProps
.Also I see some confusion between types, as if you are showing us bits of code that do not belong together or that have been edited in between. Sometimes an object itself seems to be an array, other times that same(?) object has a property that contains an array.
Anyway, when you use this prop in
<MenuContent subMenuItems={subMenuItems} />
, it is not compatible with themenuListProps
that it expects.You have an “if" to "guard the usage", but that is not a kind of type guard or type narrowing that Typescript can recognize.
If you change it to this:
then I expect at least the mentioned error to go away, because now Typescript knows that inside the "if" the variable can not be
undefined
.However there is more to be done, but until you provide consistent code, I can not comment on what you should actually use everywhere.