Skip to content

Invalid type error with discriminatedUnion and or #1761

Discussion options

You must be logged in to vote

Is this what you are looking for?

export const schema = z.object( {
    items: z.array(
        z.discriminatedUnion( 'type', [
            ...productItem.options,
            contentItem,
            documentItem
        ] )
    ),
} )

Here's the full example:

const productItemBase = z.object( {
    type: z.literal( 'product' ),
    title: z.string(),
} )

const productItem = productItemBase
    .extend( { url: z.string() } )
    .or( productItemBase.extend( { description: z.string() } ) )

const contentItem = z.object( {
    type: z.literal( 'content' ),
    title: z.string(),
    url: z.string(),
    imageUrl: z.string(),
} )

const documentItem = z.object( {
    type: z.literal( 'docu…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
3 replies
@JacobWeisenburger
Comment options

@rogue-kitten
Comment options

@standuprey
Comment options

Comment options

You must be logged in to vote
1 reply
@davidoort
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #1757 on December 26, 2022 22:59.