Skip to content

Is there a way omit keys from a discriminated union? Or an alternative way to achieve something similar? #1434

Answered by scotttrinh
kculmback-eig asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, we've talked previously about adding the other ZodObject methods to ZodDiscriminatedUnion, but haven't made any progress on that that I'm aware of. The temporary fix is to make a separate union like this:

TypeScript Playground

const createOmits = {
  id: true,
  createdOn: true,
  lastUpdated: true,
} as const;

export const CreateTemplateSchema = z.discriminatedUnion("type", [
  CommunicationTemplateSchema.omit(createOmits),
  NoteTemplateSchema.omit(createOmits),
]);

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@kculmback-eig
Comment options

Answer selected by kculmback-eig
Comment options

You must be logged in to vote
0 replies
Comment options

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