import { describe, expectTypeOf, test } from "vitest"; import { z } from "zod"; import type { $ZodString } from "zod/v4/core"; import { defineGenericFn, defineTypedFn } from "./fn-helpers.js"; describe("defineTypedFn", () => { test("should work with complex schemas", () => { const presetSchema = z .object({ id: z.number(), name: z.string(), age: z.number(), status: z.boolean(), gender: z.union([ z.literal("male"), z.literal("female"), z.literal("other"), ]), detail: z.object({ email: z.string().email(), birthday: z.date(), }), }) .describe("User"); type PresetData = z.infer; const luckUserFilter = defineTypedFn({ name: "Luck User", define: z.function({ input: [presetSchema], output: z.boolean() }), implement: (value) => { return value.id < 10 && Math.random() > 0.5; }, }); expectTypeOf(luckUserFilter.implement).toEqualTypeOf< (input: PresetData) => boolean >(); }); }); describe("defineGenericFn", () => { test("should work with genericLimit", () => { const fn = defineGenericFn({ name: "Equals", genericLimit: (t): t is $ZodString => t._zod.def.type === "string", define: (t) => z.function({ input: [t, t], output: z.boolean(), }), implement: (value: string, target: string) => value === target, }); expectTypeOf(fn.implement).toEqualTypeOf< (data1: string, data2: string) => boolean >(); }); });