Use .nullish() instead of .optional().nullable() in zod

8/16/2025

#zod#typescript

When working with zod, you might have found yourself using .optional().nullable() to make a field optional and nullable. Especially when you are working with tRPC or similar API libraries, this is a common case for API input validation.

But there is a better way to do this.

Instead of using .optional().nullable(), you can use .nullish().

const schema = z.object({ 
    name: z.string().optional().nullable(), // ❌ don't do this
    name: z.string().nullish(), // ✅ do this instead
});

New: Your Idea. Our Experts. We'll Build Your SaaS For You.

Our 'Done For You' service simplifies the process, taking your concept and delivering a production-ready MVP or complete SaaS solution.

Start your scalable and production-ready SaaS today

Save endless hours of development time and focus on what's important for your customers with our SaaS starter kits for Next.js, Nuxt 3 and SvelteKit

Get started

Stay up to date

Sign up for our newsletter and we will keep you updated on everything going on with supastarter.

    Use .nullish() instead of .optional().nullable() in zod | supastarter - SaaS starter kit for Next.js and Nuxt