admin管理员组文章数量:1435859
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?
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?
Share Improve this question edited Nov 15, 2024 at 21:32 Tomasz asked Nov 15, 2024 at 21:26 TomaszTomasz 551 silver badge5 bronze badges 1 |1 Answer
Reset to default 2First a NOTE: The error shows that
subMenuItems={subMenuItems}
is being passed as props, but the code that you give us later shows that (same?) line as<MenuContent passedMenuItems={subMenuItems} />
, so with a different prop name.Please FIX the question for this aspect.
Because of the error, I will assume that
<MenuContent subMenuItems={subMenuItems} />
is actually being used.
Your type menuItemProps
defines the property as subMenuItems?: menuListProps
which means two things:
- The property is optional, it does not need to be specified when creating an instance;
- The actual type of the property becomes
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 the menuListProps
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:
if (subMenuItems !== undefined)
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.
本文标签: javascriptType 39undefined39 is not assignable to type 39menuItemProps39Stack Overflow
版权声明:本文标题:javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745675952a2669851.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
subMenuItems?: menuListProps
but your data is an array. You could change it tosubMenuItems?: MenuItem[]
. – keithwalsh Commented Nov 15, 2024 at 23:16