Skip to content

Instantly share code, notes, and snippets.

@T2Knock
Forked from ciiqr/zod-optional-null.ts
Created October 2, 2023 09:58
Show Gist options
  • Save T2Knock/7b66eb929833d4e5c8f332d74f7e8f26 to your computer and use it in GitHub Desktop.
Save T2Knock/7b66eb929833d4e5c8f332d74f7e8f26 to your computer and use it in GitHub Desktop.
zod optional/nullable/nullish differences
// zod schema
z.object({
// valid if string or:
optional: z.string().optional(), // field not provided, or explicitly `undefined`
nullable: z.string().nullable(), // field explicitly `null`
nullish: z.string().nullish(), // field not provided, explicitly `null`, or explicitly `undefined`
});
// type
{
optional?: string | undefined;
nullable: string | null;
nullish?: string | null | undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment