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<42>(); 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>>(); });