#targeting nav item reference value slug

1 messages · Page 1 of 1 (latest)

winter sandal
#

Hi as title shows,

I am trying to target the nav items.link.reference.value.slug but when i put slug in it says its not a valid type

so i looked at the payload automatic generated types and its showing this:
value: string | Page;

if i remove string | it works as expected

but obviously when i add new custom blocks etc and run generate types itll be overridden, am i being silly and am i missing something?

open elmBOT
simple timber
#

Can you post the code youre using to reference it

winter sandal
#

{header?.navItems?.map((navItem, index) => {

    return (
      <Link href={navItem?.link?.url || navItem.link.reference.value.slug} key={`navMenu${index}`}>
        {navItem.link.label}
      </Link>
    )
  })}
#

error: Property 'slug' does not exist on type 'string | Page'.
Property 'slug' does not exist on type 'string'.

simple timber
#

So you want to fix the type

winter sandal
#

if i remove string from the type reference and just have page in there

#

it works

#

but it auto generates with string | page

simple timber
#

Well yeah but we dont know if its meant to be there

winter sandal
#

im new to ts tbh so apologies if this is a newb error

simple timber
#

You could get around it in a hacky way by using ( navItem.link.reference.value as Page).slug

#

but that assumes navItem.link.reference.value will always be Page

#

and if it IS ever a string, it will error

winter sandal
#

ahhh

#

that works!

#

genius

simple timber
#

Haha well do you understand though why it's not a REAL solution

#

Like, it may be fine

winter sandal
#

yeah its basically a work around for TS

simple timber
#

Correct

winter sandal
#

its undoing the type safety

simple timber
#

But there are unknowns

#

maybe the generated type initially was wrong and the types need to be regenerated

#

there are a bunch of variables

#

But in any case

#

If you feel confident it will fix it without conflicts

#

Then roll with it :d

winter sandal
#

cheers dude