Extracting type from block results in "never", always (hehe)

default discord avatar
steveninety
last month
2
  • default discord avatar
    diesieben07
    last month
    Page['blocks']

    is an array, which has no overlap with

    { blockType: "collectionItems" }

    , that's why you get

    never

    .


    What you want is

    Page['blocks'][number]

    to get the elements of the array. This however doesn't work either, because

    Page['blocks']

    is optional. You can fix that with

    NonNullable

    :


    type Hero = Extract<NonNullable<Page['blocks']>[number], { blockType: "collectionItems" }>
  • default discord avatar
    steveninety
    last month

    That answers my question, thanks for this typescript lesson 🙂

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.