colinhacks/zod

Feature request: Support pick on unions, using shared properties (just like in TS)

alexbjorlig opened this issue · 0 comments

const someType = z.union([
    z.object({shared: z.number(), foo: z.array(z.string()) }),
    z.object({shared: z.number() })
  ])

type SomeType = z.infer<typeof someType>;

type ThisTypeWorksInTs = Pick<SomeType, 'shared'>;

/*
Gives error Property 'pick' does not exist on type
'ZodUnion<[ZodObject<{ shared: ZodNumber; foo: ZodArray<ZodString, "many">; },
"strip", ZodTypeAny, { shared: number; foo: string[]; }, { shared: number; foo: string[]; }>,
ZodObject<...>]>'.ts(2339)
*/
const butDoesNotWorkInZod = someType.pick({ shared: true });