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.

Object properties are all optional by default

See original GitHub issue

import * 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:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
DavidR95commented, Nov 30, 2021

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 had strictNullChecks: 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.

1reaction
scotttrinhcommented, Nov 30, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

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