Object properties are all optional by default
See original GitHub issueimport * as Zod from "zod";
const Dog = Zod.object({
name: Zod.string(),
age: Zod.number(),
});
type Dog = Zod.infer<typeof Dog>;
Copied this code straight from the docs. The type of “Dog” is { name?: string, age?: number }. In the docs it says it should be { name: string, age: number }
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
All object properties are optional by default #498 - GitHub
const User = z.object({ username: z.string(), }) type User = z.infer Current result type User = { username?: string } Expected result type ......
Read more >Function that takes an object with optional/default properties ...
The default value for a parameter is just that - a default value. It doesn't handle merging properties of objects that are passed...
Read more >Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining ( ?. ) operator accesses an object's property or calls a function. If the object accessed or function called is ......
Read more >Optional object type attributes with defaults in v1.3 alpha ...
Hi all :blush:, I'm the Product Manager for Terraform Core, and we're excited to share our v1.3 alpha , which includes the ability...
Read more >How To Access JavaScript Object Properties | Default Value
You'll also see how to check for null in Object properties using Optional Chaining. You'll also learn about setting default value if Null...
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
In case anyone had the same issue as me (with a new Nest project, in this instance), I thought I had enabled
strict: true
, but I also hadstrictNullChecks: false
set separately, which was making all properties optional. Just needed to enable that too (or remove it, as it’s the default) and everything worked fine.@srounce
After checking that you don’t accidentally have
strictNullChecks: false
(thanks @DavidR95!) can you verify that they’re using the same version of Zod and TypeScript? As you noted,infer
is working within a single package/module boundary, so it must be something about how dependencies are being resolved and/or how typescript is handling the imported type definitions.