Skip to content

Instantly share code, notes, and snippets.

@ciiqr
Last active April 24, 2025 20:02
Show Gist options
  • Save ciiqr/ee19e9ff3bb603f8c42b00f5ad8c551e to your computer and use it in GitHub Desktop.
Save ciiqr/ee19e9ff3bb603f8c42b00f5ad8c551e 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;
}
@zechdc
Copy link

zechdc commented May 22, 2024

Thanks!

@rodrigoaraujo7
Copy link

Thanks, very good!

@filippofilip95
Copy link

Niceee :)

@marcelocmenezes
Copy link

Nice!

@adwait-godbole
Copy link

Thanks a lot!

@khobiziilyes
Copy link

Danke!

@JohayerChowdury
Copy link

Thanks!

@bay007
Copy link

bay007 commented Jul 24, 2024

awesome

@matheuspl
Copy link

Thanks!

@maRci002
Copy link

I thought I can chain like this:

field: z.string().nullable().optional(),

But this is the way:

field: z.string().nullish(),

@b3nab
Copy link

b3nab commented Aug 30, 2024

I thought I can chain like this:

field: z.string().nullable().optional(),

But this is the way:

field: z.string().nullish(),

Yes, I came to the same conclusions as you, .nullish() is what I was searching instead of .optional().

@Mandalorian007
Copy link

Thank you

@mhgamboa
Copy link

Adding to the thanks train. Thank you!

@asonni
Copy link

asonni commented Oct 12, 2024

Thank you

@crowbel
Copy link

crowbel commented Oct 15, 2024

Thanks πŸ™Œ

@tiredpyrx
Copy link

adamsΔ±n len

@Programador6MCS
Copy link

Thanks a lot. Real heros don't wear capes, they post wisdom on GitHub

@doei
Copy link

doei commented Oct 27, 2024

thank you :)

@Geisson19
Copy link

thank you!

@IlhomYuldoshev
Copy link

thank you :)

@flunetS
Copy link

flunetS commented Nov 29, 2024

thank you !!!

@dylan1951
Copy link

thanks

@temich
Copy link

temich commented Dec 18, 2024

πŸ‘

@eric-lesslie
Copy link

Thanks!

@sCuz12
Copy link

sCuz12 commented Jan 17, 2025

Thanks !!

@krizh-p
Copy link

krizh-p commented Feb 2, 2025

πŸ‘

@ogrotten
Copy link

Here's another "Thank You".

@bramswenson
Copy link

πŸ₯‡

@RadoslavTsvetanov
Copy link

bro thanks a bunch

@becelli
Copy link

becelli commented Mar 30, 2025

Obrigado! πŸ‡§πŸ‡·

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment