Skip to content

Impossible to infer catchall schema from a z.ZodObject #5372

@qraynaud

Description

@qraynaud

In v3 it was possible to infer the z.Schema of the catchall schema on a zodobject by doing type CatchAll = (typeof objectSchemaWithCatchall)["_def"]["catchall"].

Since v4 there is a regression and this attribute is always z.Schema (without more precise typing). I was maintaining a tool mapping a schema to some other type and I can’t port this part of the code to the v4. Would it be possible to find a way to properly type this attribute on Schemas in some way?

Right now, config is provided with z.core.$catchall<Schema> that creates a { in: Record<any, z.input<Schema>, out: Record<any, z.output<Schema> }. I was thinking all it would need would be to add catchall to it: { in: Record<any, z.input<Schema>, out: Record<any, z.output<Schema>, catchall: Schema }.

It would allow to do:

export type ExtractCatchall<T extends z.ZodObject> = T extends z.ZodObject<infer _S, infer Config>
  ? keyof Config extends 'catchall'
    ? Config['catchall']
    : never

Is this something you would be interested in? I would gladly make a PR if you want.

Thanks a lot for v4 anyway, it’s so much better than v3! I love it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions