Skip to content

How can I do conditional types, mapped types, and optional key modifiers (+?)? #1635

Discussion options

You must be logged in to vote

This is the best I could do with what you provided. I don't know what DBFilterColOptions is so I wasn't able to include that in this solution. If you can provide that, I can update the solution.

type FilterOptionOperators = 'and' | 'or' | 'not'

type DBFilterCompoundOptions = {
    [ P in FilterOptionOperators ]+?: P extends 'not' ? DBFilterCompoundOptions : DBFilterCompoundOptions[]
}

const dbFilterCompoundOptionsSchema: z.ZodType<DBFilterCompoundOptions> = z.lazy( () =>
    z.object( {
        and: z.array( dbFilterCompoundOptionsSchema ),
        or: z.array( dbFilterCompoundOptionsSchema ),
        not: dbFilterCompoundOptionsSchema,
    } ).deepPartial()
)

console.log(
    dbFilter…

Replies: 2 comments 1 reply

Comment options

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

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants