Lesson
Declare Lack of Value in zod (optional, nullable and nullish)
use Zod for TypeScript schemas, including handling undefined, null, and special types like optional and nullable values, as well as any, unknown never and void
- Access
- Included
- Transcript
- Needs source
In this lesson we discuss how Zod works with TypeScript to handle special types like undefined, null, any, unknown, never and void.
We modify our room booking schema to demonstrate the use of optional and nullable Zod utilities.
By using .optional, we allow a property to be either its original type or undefined. With .nullable, we allow it to be either its original type or null. Finally, .nullish allows a value to be either its original type, null, or undefined.
For all cases, we run tests to show how these schemas work in practice, as well as how TypeScript infers types.