0

toZod is a utility for defining Zod schemas that agree with a TypeScript type. It basically gives you auto-suggested properties for your schema by supplying toZod with a type. If your type ever changes, it will show an error notifying you that you need to change your schema accordingly.

I have successfully used toZod on types in the past, but now I'm trying to use toZod on a type that was inferred using TRPC's inferRouterOutput inference helper. I'm getting some kind of 'strip' error and none of my types are appearing when I press ctrl + space like I would usually get.

This is the code I currently have:

type RouterOutput = inferRouterOutputs<AppRouter>;
type DefinitionTree = RouterOutput["product"]["getProductDefinitionTree"];
    
const treeSchema: toZod<DefinitionTree> = z.object({
   //properties should be showing up as suggestions (via ctrl + space)
}).array();

Upon hovering my mouse over 'TreeSchema', I get a message that shows the variable and then shows a type error message. I see the word "strip" in the error message so I'm wondering if that is a clue to what the issue is?

Here is the variable

const treeSchema: z.ZodArray<z.ZodObject<{
    product: z.ZodNullable<z.ZodObject<{
        id: z.ZodString;
        name: z.ZodNullable<z.ZodString>;
        *more properties etc....*
    }, "strip", z.ZodTypeAny, {
       ...;
    },{
    ...;
    }>, "many">

Here are all of the errors

Type 'ZodArray<ZodObject<{}, "strip", ZodTypeAny, {}, {}>, "many">' is
not assignable to type 'ZodArray<ZodObject<{ product:
ZodNullable<ZodObject<{ id: ZodString; name: ZodNullable<ZodString>;
description: ZodNullable<ZodString>; createdById:
ZodNullable<ZodString>; ... 4 more ...; updatedAt: ZodNullable<...>;
}, "strip", ZodTypeAny, Product, Product>>; ... 31 more ...;
competitorReference: ZodNullable<...> ...'.
Type 'ZodObject<{}, "strip", ZodTypeAny, {}, {}>' is not assignable
to type 'ZodObject<{ product: ZodNullable<ZodObject<{ id: ZodString;
name: ZodNullable<ZodString>; description: ZodNullable<ZodString>;
createdById: ZodNullable<ZodString>; ... 4 more ...; updatedAt:
ZodNullable<...>; }, "strip", ZodTypeAny, Product, Product>>; ... 31
more ...; competitorReference: ZodNullable<...> | ZodNull...'.
Type '{}' is missing the following properties from type '{ product: ZodNullable<ZodObject<{ id: ZodString; name:
ZodNullable<ZodString>; description: ZodNullable<ZodString>;
createdById: ZodNullable<ZodString>; ... 4 more ...; updatedAt:
ZodNullable<...>; }, "strip", ZodTypeAny, Product, Product>>; ... 31
more ...; competitorReference: ZodNullable<...> | ZodNullable<...>;
}': product, supplierData, models, existingDrawings, and 29 more.
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
tdev
  • 45
  • 1
  • 3
  • 9
  • I had this issue, turns out I had two versions of zod in my monorepo workspace. I remove the newer version of zod and left the older version`3.20.6`, this fixed the error. – Master Ace Mar 07 '23 at 21:59
  • @MasterAce, this doesn't seem to be working for me. Any other suggestions? Think I'm doing something else wrong? Starting to get desperate. – tdev Mar 15 '23 at 00:44

0 Answers0