question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ZodObject not inferred when using ZodSchema

See original GitHub issue

I’m trying to extend a ZodObject schema which is bound to an existing interface with ZodSchema, however the schema is detected as ZodType which does not have the extend() method.

Is there some way to specify that the ZodSchema is specifically a ZodObject?


const ActiveServiceModel: ZodSchema<ActiveService> = z.object({
  service_id: z.string(),
  team_id: z.string().uuid()
})

// This fails:
const ExtendedActiveServiceModel = ActiveServiceModel.extend({
  bar: z.string()
})

Error Detail

TS2339: Property 'extend' does not exist on type 'ZodType '.
(ActiveServiceModel: z.ZodType<ActiveService, z.ZodTypeDef, ActiveService>)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
prescience-datacommented, Jan 20, 2022

@JRGranell this is very cool:


type Properties<Input> = Required<{
        [K in keyof Input]: z.ZodType<Input[K], any, Input[K]>;
}>

thanks for that 🤙🏻

0reactions
JacobWeisenburgercommented, Feb 23, 2022

This seems like this question was answered. May I close this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typecheck schemas against existing types · Issue #372 - GitHub
I would love to have a way to ensure that a Zod schema matches an existing type definition using the normal Typescript type...
Read more >
Schema validation in TypeScript with Zod - LogRocket Blog
In this article, you will learn about schema design and validation in Zod and how to run it in a TypeScript codebase at...
Read more >
Why does Zod make all my schema fields optional?
This might be caused by not using strict: true in TypeScript compiler options as referred to installation section in README file.
Read more >
TypeScript schema validation with Zod - Iskander Samatov
Zod is designed to be TypeScript-first, meaning that the library will automatically infer the static TypeScript type for your data structures.
Read more >
Designing the perfect Typescript schema validation library
But it's very uncool for the inferred type to not actually reflect ... For instance, consider how to define an object schema with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found