(typescript noob) How can I use typescript's Extract utility to extract a Block based on its blockType, without getting never?
Example type:
type Hero = Extract<Page['blocks'], { blockType: "homepage_hero" }>Reference code from Payload repo:
https://github.com/payloadcms/payload/blob/dd0ff50621fd072a87a96237311d01f28c5b17ca/templates/website/src/app/_blocks/CallToAction/index.tsxPlayground:
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" }>That answers my question, thanks for this typescript lesson 🙂
Star
Discord
online
Get dedicated engineering support directly from the Payload team.