Skip to content
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

Zod infers undefined field as optional #2598

Open
kana-sama opened this issue Jul 20, 2023 · 2 comments
Open

Zod infers undefined field as optional #2598

kana-sama opened this issue Jul 20, 2023 · 2 comments
Labels
bug-confirmed Bug report that is confirmed

Comments

@kana-sama
Copy link

kana-sama commented Jul 20, 2023

Given:

zod 3.21.4

({ }) satisfies { x: undefined };   // 1
({ }) satisfies z.infer<typeof x>;  // 2
const x = z.object({ x: z.undefined() });

Expected:

  • 1 should not typecheck because field is not optional
  • 2 should not typecheck as well because x models the same type

Actual:

  • 1 typechecks (and it is correct)
  • 2 does not typecheck (zod for unknown reason makes field optional)

It is not the problem with strict: true in tsconfig.

@kana-sama kana-sama changed the title Zod infers undefined|T fields as optional Zod infers undefined field as optional Jul 20, 2023
@dplantera
Copy link

#2464

sadly this is already known and unresolved.

@maurer2
Copy link

maurer2 commented Jul 27, 2023

Does exactOptionalPropertyTypes in the compiler options in tsconfig.json have any effect on this, e.g.?

{
  "compilerOptions": {
    "exactOptionalPropertyTypes": true
  }
}

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-confirmed Bug report that is confirmed
Projects
None yet
Development

No branches or pull requests

4 participants