Type inference issue for Object schemas
See original GitHub issueFirst, Thank you very much for your awesome work 👍
I figured out when I’m using z.infer
on an object schema, the inferred type has all the object’s attributes optional. According to the doc, all the attributes should be required.
Example from the doc:
const dogSchema = z.object({
name: z.string(),
neutered: z.boolean(),
});
type Dog = z.infer<typeof dogSchema>;
/*
equivalent to:
type Dog = {
name:string;
neutered: boolean;
}
*/
But this is what I get:
type Dog = {
name?: string;
neutered?: boolean;
}
I’m using zod 1.5.0
and I’ve also tried with 1.3.0
but keep getting the Dog type with optional attributes.
Thanks for your help!
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to infer types of an object created from a schema?
You can do this, using a mapped type, but first you need to preserve the original type of the schema. If you add...
Read more >Typescript type inference from mongoose Schema · Issue #9715
Given a type Def which represents a schema definition, Entity works by mapping each of its fields into a "non-schema" field (e.g. String...
Read more >How to Infer Types from Schema : r/typescript - Reddit
So yes, you can start from schema and infer a type for objects that meet this schema, as well as generate a reasonably-typed...
Read more >Type Inferability and Decidability of the Security Problem ...
The security problem against inference attacks has been formalized under a model of object-oriented databases called method schemas.
Read more >Algebraic types and schema inference - Chapeau
In particular, schema inference can suffer when you're ingesting a dataset of heterogeneous objects. In this post, I'd like to sketch out ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks so much @vriad
Love this library and the support!
@talmr-k forgot to tag you. Here’s a description on how to set up multiple tsconfigs: https://stackoverflow.com/questions/37579969/how-to-use-multiple-tsconfig-files-in-vs-code