You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The other day I needed to use a zod schema which defined either all of the properties in an object or none of them. Basically something like
constExampleSchema=z.object({openingHours: z.date(),closingHours: z.date()});typeExample=z.infer<typeofExampleSchema>;// we want this to be correctconstexample: Example={openingHours: newDate(),closingHours: newDate(),};// we want this to be correctconstexample: Example={};// we want this to be incorrectconstexample: Example={closingHours: newDate(),};
Just creating a schema like that was not enough for me, so I jumped ahead and created a factory that generates such schemas based on the object with required properties. The code turned out to be pretty short and easy, the type definitions were correct, I was happy.
Type 'k_1' cannot be used to index type 'addQuestionMarks<{ [k in keyof T]: T[k]["_output"]; }>'. at line 17
Type 'k_3' cannot be used to index type 'addQuestionMarks<{ [k_2 in keyof T]: T[k_2]["_input"]; }>'. line 26
and so on.
The generated type uses zod's complex types and after being compiled they seem incorrect and full of error messages. I took a look myself and began to understand why they are wrong but can't seem to work out why they are generated in such a way that they are now wrong although no indication of errors is present in the code. Maybe it's some incompatibility between my tsconfig and zod? I also setup a minimal working example in a git repo so you can reproduce the problem and check out everything in piece and quiet. Here's the repo: https://github.com/18ivan18/zod-create-all-or-nothing-schema.
Every piece of advice is welcome.
The text was updated successfully, but these errors were encountered:
The other day I needed to use a zod schema which defined either all of the properties in an object or none of them. Basically something like
Just creating a schema like that was not enough for me, so I jumped ahead and created a factory that generates such schemas based on the object with required properties. The code turned out to be pretty short and easy, the type definitions were correct, I was happy.
The problem comes when I tried to compile it with
tsc
.tsconfig.json
lib/typedefs/define-all-or-nothing-schema.d.ts
and the errors:
Type 'k_1' cannot be used to index type 'addQuestionMarks<{ [k in keyof T]: T[k]["_output"]; }>'.
at line 17Type 'k_3' cannot be used to index type 'addQuestionMarks<{ [k_2 in keyof T]: T[k_2]["_input"]; }>'.
line 26and so on.
The generated type uses zod's complex types and after being compiled they seem incorrect and full of error messages. I took a look myself and began to understand why they are wrong but can't seem to work out why they are generated in such a way that they are now wrong although no indication of errors is present in the code. Maybe it's some incompatibility between my tsconfig and zod? I also setup a minimal working example in a git repo so you can reproduce the problem and check out everything in piece and quiet. Here's the repo: https://github.com/18ivan18/zod-create-all-or-nothing-schema.
Every piece of advice is welcome.
The text was updated successfully, but these errors were encountered: