Replies: 6 comments 8 replies
-
I'm not sure that the question is very clear on what you are wanting? What do you mean by: "without any ties to zod"? Can you not just |
Beta Was this translation helpful? Give feedback.
-
In the Ecosystem section of the docs, it shows other libraries that might do what you are asking.
|
Beta Was this translation helpful? Give feedback.
-
It's a bit convoluted... But you could use the zod-to-json-schema package and then a json-schema-to-ts package or tool such as this one. It should work for most cases, except when there is no logical overlap (such as promises, functions and maps/sets etc). |
Beta Was this translation helpful? Give feedback.
-
I decided to make a library for this, check it out here (zod-to-ts). It converts your schema into a TS AST node. |
Beta Was this translation helpful? Give feedback.
-
This is such a common use case. How you all use backend types in your frontend if not by this? |
Beta Was this translation helpful? Give feedback.
-
This is impossible to do consistently at runtime, without typescript compiler fuckery. Consider this code: const schema = z.string().transform(() => 80085);
const value = schema.parse("test"); Runtime analyzers will say that import { zodToTs, printNode } from "zod-to-ts";
console.log(printNode(zodToTs(schema, "schema").node));
// Outputs `string`. At runtime console.log(typeof schema.parse("test"));
// Outputs `number`. If you need your schemas in the frontend and in the backend -- move them to an external package, and accept that you will have to ship at least the zod types in the frontend. If your treeshaker is good, then zod code will not enter your final js bundles. Or if you are sure that you will not use any transformations, then you can try using |
Beta Was this translation helpful? Give feedback.
-
I've made a ZOD schema and have an Inferred Type. Now I need to provide this resulting type to the users of our library without any ties to ZOD. Can someone please advise how do I do this?
Currently, generated d.ts file looks like this:
I'm looking for an elegant solution to produce this:
I'm open to any suggestions how to do it. If none are found - I may eventually experiment with TS compiler API and see if I can extract types that way.
Beta Was this translation helpful? Give feedback.
All reactions