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

Return type of .default() should not be nullable #1456

Closed
pokey opened this issue Oct 3, 2022 · 2 comments
Closed

Return type of .default() should not be nullable #1456

pokey opened this issue Oct 3, 2022 · 2 comments

Comments

@pokey
Copy link

pokey commented Oct 3, 2022

If I call .default(), why does it make the output type nullable? If I have provided a default value, shouldn't the output never be null?

@ealmansi
Copy link

ealmansi commented Oct 6, 2022

can't reproduce on the latest version; adding default does not make the output type nullable:

// v3.19.1
import { z } from "zod";

const schema = z.object({
  x: z.string().default("str"),
});

export type Schema = z.infer<typeof schema>;
// type Schema = {
//   x: string;
// }

@scotttrinh
Copy link
Collaborator

It's possible that this is a misunderstanding as well: default works like JavaScript default assignment in destructuring: it replaces undefined with the default value. So if you input schema is nullable, adding a default does not make it non-nullable, it just allows the input to be optional (T | undefined) and makes the output non-optional (T).

Closing since this is working as intended, but feel free to post follow-up questions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants