Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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

default discord avatar
steveninetylast year
2
  • default discord avatar
    diesieben07last year
    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
    steveninetylast year

    That answers my question, thanks for this typescript lesson 🙂

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.