skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

    subMenuItems?: NavItemProps["menuItem"][] // instead of subMenuItems?: NavItemProps[]
    

    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?

    type NavItemProps = {
      menuItem: {
        id: number;
        title: string;
    
        hideForHamburgerMenu?: boolean;
    
        subMenuItems?: NavItemProps["menuItem"][];
      }
    }
    
    function NavItem (props: NavItemProps){
        
        const { menuItem } = props;
        const { id, title, subMenuItems, hideForHamburgerMenu } = menuItem;
    
        if (hideForHamburgerMenu)
            return (<></>)
        else
            return (
    
                <li key={id}>
    
                    <h2>{title}</h2>
    
                    {subMenuItems && (
                      <ul>
                        {subMenuItems.map((subItem) => (
                          <NavItem key={subItem.id} menuItem={subItem} />
                        ))}
                      </ul>
    
                    )}
                </li>
                )
    }
    
    export default function MobileAsideMainMenu(){
    
        return(
            <>
                <nav>
                    <ul>
                        {menuItems.map((menuItem) => (
                            <NavItem key={menuItem.id} menuItem={menuItem} />
                        ))}
                    </ul>
                </nav>
            </>
    
        )
    }
    

  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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search