最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow

matteradmin22PV0评论

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
  • Type definition is subMenuItems?: menuListProps but your data is an array. You could change it to subMenuItems?: MenuItem[]. – keithwalsh Commented Nov 15, 2024 at 23:16
Add a comment  | 

1 Answer 1

Reset to default 2

First 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:

  1. The property is optional, it does not need to be specified when creating an instance;
  2. 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.

Post a comment

comment list (0)

  1. No comments so far