Use ts type in Zod

i have a type that i am importing from a 3rd party package. is there a way i can use this type and put it my object schema. if i just make it an enum and i update the 3rd party package and they change these types, my code would be wrong
// imported type:
type EmailOtpType = 'signup' | 'invite' | 'magiclink' | 'recovery' | 'email_change' | 'email'

const mySchema = z.object({
type: /* use imported EmailOtpType here */,
});
// imported type:
type EmailOtpType = 'signup' | 'invite' | 'magiclink' | 'recovery' | 'email_change' | 'email'

const mySchema = z.object({
type: /* use imported EmailOtpType here */,
});
2 Replies
.The_bl | 0x662c
@Joey9 do like this const emailTypes = ['func1', 'func2', 'func3'] as const; const mySchema = z.object({ type: z.enum(emailTypes), });

Did you find this page helpful?