zod
zod copied to clipboard
Please add TypeScript support for `.extend()` method in the z.Schema type
I find it very useful to add my types for Zod schemas, so that if I change the type, TypeScript helps me in highlighting the errors in my broken schemas. But as soon as I assign any schema z.Schema<MyType>, I can no longer extend this schema.
Here's a detailed example:
- Without
z.Schema<MyType>specification:
const mySchema = z.object({ ... });
const anotherSchema = mySchema.extend({ ... });
Works just fine, but no highlights if I have my types mismatched with the schema.
- With
z.Schema<MyType>specification:
const mySchema: z.Schema<MyType> = z.object({ ... });
const anotherSchema = mySchema.extend({ ... });
Here, I have my type definitions connected to Zod schemas, but I can no longer safely extend from mySchema (by safely, I mean type-safety, if I put @ts-ignore, it works just as expected).
Please suggest a workaround if any exists or add a type support for extend method in Schema type definition. Thanks!
can't agree more,I asked the question too。https://github.com/colinhacks/zod/issues/1271
You can do this using
const anotherSchema:z.Schema<AnotherType> = mySchema.and(z.object({ ... }));
Also for z.ZodType
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.