Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract the inferred type is not working as expected #2696

Closed
wesleyfeitosa opened this issue Aug 31, 2023 · 3 comments
Closed

Extract the inferred type is not working as expected #2696

wesleyfeitosa opened this issue Aug 31, 2023 · 3 comments

Comments

@wesleyfeitosa
Copy link

wesleyfeitosa commented Aug 31, 2023

In documentation says that we can infer the type from a schema like this:

image

But when I try this with the example above, I was expecting attributes required by default and optional only for those I set as optional...

import { z } from 'zod';

export const createDiscountCouponSchema = z.object({
  accountId: z.number().optional(),
  coupon: z.string(),
  additionalDiscount: z.number(),
  expirationDate: z.date(),
  status: z.string(),
  campaign: z.string().optional(),
});

export type CreateDiscountCouponDto = z.infer<
  typeof createDiscountCouponSchema
>;

however, I'm receiving all attributes as optionals,

image

is that right?

I'm using zod 3.22.2 and typescript 5.0.3

@antonmoiseev
Copy link

I have the same issue. zod - 3.22.2 (tried with 3.21.4 as well), typescript - 5.1.6

@dudubernardino
Copy link

it works for me when i changed strict to true, take a look in docs about that requirements

// tsconfig.json
{
  // ...
  "compilerOptions": {
    // ...
    "strictNullChecks": true
  }
}

@wesleyfeitosa
Copy link
Author

it works for me when i changed strict to true, take a look in docs about that requirements

// tsconfig.json
{
  // ...
  "compilerOptions": {
    // ...
    "strictNullChecks": true
  }
}

Thanks @dudubernardino, it worked for me

Repository owner locked and limited conversation to collaborators Sep 22, 2023
@JacobWeisenburger JacobWeisenburger converted this issue into discussion #2772 Sep 22, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants