Lesson
Use parse and safeParse Runtime Type-checks with zod
use zod parse function to conditionally throw ZodError, use zod safeParse to get a boolean value and run typescript type guard over success property
- Access
- Included
- Transcript
- Needs source
You might want to run zod schema checks in different styles.
Sometimes you prefer to conditionally throw the error - that's what parse does. But another time you'd prefer to receive a boolean property that determines whether the validation was successful or not - and perform further actions accordingly.
Additionally, the error-throwing API of parse works extremely well with TypeScript assertion functions. Their combination is just... perfect! 👌
That's what we learn in this lesson.