zod brand primitive types

Youtube suggested great video The HIDDEN GEM in Zod & Valibot You NEED To Use

Key idea is that whenever you have function like this:

function demo(email: string, uuid: string) {}

you are actually not controlling primitive types passed to the function

and with zod, you can

here is an example

import z from "zod";

const EmailSchema = z.email().brand("Email");
const UuidSchema = z.uuid().brand("UUID");

type Email = z.infer<typeof EmailSchema>;
type UUID = z.infer<typeof UuidSchema>;

function demo(email: Email, id: UUID) {
  console.log(`Email: ${email}, UUID: ${id}`);
}

// demo("user@example.com", "123e4567-e89b-12d3-a456-426614174000"); // fails
// Argument of type 'string' is not assignable to parameter of type 'string & $brand<"Email">'.
//   Type 'string' is not assignable to type '$brand<"Email">'.

const email = EmailSchema.parse("user@example.com");
const id = UuidSchema.parse("123e4567-e89b-12d3-a456-426614174000");

demo(email, id); // works

with this in place we can pass validated primitives accross our app