zod icon indicating copy to clipboard operation
zod copied to clipboard

How would I extract first property type from zod object?

Open gajus opened this issue 3 years ago • 1 comments

Here is what I got:

import {
  z,
  type ZodTypeAny,
} from 'zod';

const foo = z.object({
  name: z.string(),
});

const bar = <T extends ZodTypeAny>(shape: T) /* ??? */ => {
  return null as any;
};

const baz = bar(foo);

console.log(baz);

I need baz to be first/any property value of foo, i.e. in this case it should be string.

How would I extract first property type from zod object?

gajus avatar Aug 04 '22 03:08 gajus

I think it should be:

const bar = <T extends ZodTypeAny>(shape: T): z.infer<T>[keyof z.infer<T>] => {
  return null as any;
};

although not 100% sure

gajus avatar Aug 04 '22 03:08 gajus