Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zod-to-ts

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-to-ts - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

1

dist/index.js

@@ -66,3 +66,2 @@ var __defProp = Object.defineProperty;

const { typeName } = zod._def;
console.log(typeName);
const getTypeType = callGetType(zod, identifier, options);

@@ -69,0 +68,0 @@ if (getTypeType && typeName !== "ZodNativeEnum") {

28

package.json
{
"name": "zod-to-ts",
"version": "0.2.1",
"version": "0.2.2",
"type": "module",

@@ -36,19 +36,19 @@ "description": "generate TypeScript types from your Zod schema",

"devDependencies": {
"@types/node": "17.0.5",
"@typescript-eslint/eslint-plugin": "5.8.1",
"@typescript-eslint/parser": "5.8.1",
"dprint": "0.19.2",
"@types/node": "17.0.21",
"@typescript-eslint/eslint-plugin": "5.12.1",
"@typescript-eslint/parser": "5.12.1",
"dprint": "0.23.0",
"esbuild-node-loader": "0.6.5",
"eslint": "8.5.0",
"eslint-config-prettier": "8.3.0",
"eslint": "8.10.0",
"eslint-config-prettier": "8.4.0",
"husky": "7.0.4",
"lint-staged": "12.1.4",
"lint-staged": "12.3.4",
"npm-run-all": "4.1.5",
"rimraf": "3.0.2",
"ts-dedent": "2.2.0",
"tsup": "5.11.9",
"typescript": "4.5.4",
"vite": "2.7.9",
"vitest": "0.0.123",
"zod": "3.11.6"
"tsup": "5.11.13",
"typescript": "4.5.5",
"vite": "2.8.4",
"vitest": "0.5.8",
"zod": "3.12.0"
},

@@ -84,3 +84,3 @@ "sideEffects": false,

},
"readme": "# zod-to-ts\n\ngenerate TypeScript types from your [Zod](https://github.com/colinhacks/zod) schema\n\n## Installation\n\n```\nnpm install zod-to-ts zod typescript\n```\n\nYou must be on zod@3 and typescript@4.\n\n## Usage\n\n```ts\nimport { z } from 'zod'\nimport { zodToTs } from 'zod-to-ts'\n\n// define your Zod schema\nconst UserSchema = z.object({\n username: z.string(),\n age: z.number(),\n inventory: z.object({\n name: z.string(),\n itemId: z.number(),\n }).array(),\n})\n\n// pass schema and name of type/identifier\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\n```\n\nYou must pass in the identifier `User` or it will default to `Identifier`. This is necessary to handle cases like recursive types and native enums. `zodToTs()` only returns the type value, not the actual type declaration. If you want to add an identifier to the type and create a type declaration, you can use the `createTypeAlias()` utility:\n\n```ts\nimport { createTypeAlias, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst typeAlias = createTypeAlias(node, identifier)\n```\n\nresult:\n\n```ts\ntype User = {\n username: string\n}\n```\n\n`zodToTs()` and `createTypeAlias()` return a TS AST nodes, so if you want to get the node as a string, you can use the `printNode()` utility.\n\n`zodToTs()`:\n\n```ts\nimport { printNode, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst nodeString = printNode(node)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```\n\"{\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\"\n```\n\n`createTypeAlias()`:\n\n```ts\nimport { createTypeAlias, printNode, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst typeAlias = createTypeAlias(node, identifier)\nconst nodeString = printNode(typeAlias)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```\n\"type User = {\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\"\n```\n\n## Overriding Types\n\nYou can use `withGetType` to override a type, which is useful when more information is needed to determine the actual type. Unfortunately, this means working with the TS AST:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType } from 'zod-to-ts'\n\nconst DateSchema = withGetType(\n z.instanceof(Date),\n (ts) => ts.factory.createIdentifier('Date'),\n)\n\nconst ItemSchema = z.object({\n name: z.string(),\n date: DateSchema,\n})\n\nconst { node } = zodToTs(ItemSchema, 'Item')\n```\n\nresult without `withGetType` override:\n\n```ts\ntype Item = {\n name: string\n date: any\n}\n```\n\nresult with override:\n\n```ts\ntype Item = {\n name: string\n date: Date\n}\n```\n\n[TypeScript AST Viewer](https://ts-ast-viewer.com/) can help a lot with this if you are having trouble referencing something. It even provides copy-pastable code!\n\n### Special Cases\n\n#### [z.lazy()](https://github.com/colinhacks/zod#recursive-types)\n\nLazy types default to referencing the root type (`User` in the following example). It is impossible to determine what it is referencing otherwise.\n\n```ts\n// Zod cannot infer types when you use the z.lazy\n// so you must define it\nimport { z } from 'zod'\ntype User = {\n username: string\n friends: User[]\n}\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n friends: z.lazy(() => UserSchema).array(),\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n```ts\ntype User = {\n username: string\n friends: User[]\n}\n```\n\nBut what happens when the schema looks like this?\n\n```ts\ntype User = {\n username: string\n item: {\n name: string\n itemId: string\n }\n friends: User[]\n}\n\n// essentially when you are referencing a different field\n// and not the root type\nconst friendItems = z.lazy(() => UserSchema.item).array()\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n item: z.object({\n name: z.string(),\n id: z.number(),\n }),\n friendItems,\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n username: string\n item: {\n name: string\n id: number\n }\n friendItems: User[]\n}\n```\n\n`friendItems` will still have the `User` type even though it is actually referencing `UserSchema[\"item\"]`. You must provide the actual type using `withGetType`:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType } from 'zod-to-ts'\ntype User = {\n username: string\n item: {\n name: string\n id: number\n }\n friends: User[]\n}\n\nconst friendItems: z.Schema<User['item'][]> = withGetType(\n z.lazy(() => UserSchema.item).array(),\n // return a TS AST node\n (ts, identifier) =>\n ts.factory.createIndexedAccessTypeNode(\n ts.factory.createTypeReferenceNode(\n ts.factory.createIdentifier(identifier),\n undefined,\n ),\n ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),\n ),\n)\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n item: z.object({\n name: z.string(),\n id: z.number(),\n }),\n friendItems,\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n```ts\n{\n username: string\n item: {\n name: string\n id: number\n }\n friendItems: User['item'][]\n}\n```\n\n#### [z.nativeEnum()](https://github.com/colinhacks/zod#native-enums)\n\n`z.enum()` is always preferred, but sometimes `z.nativeEnum()` is necessary. `z.nativeEnum()` works similarly to `z.lazy()` in that the identifier of the enum cannot be determined:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType } from 'zod-to-ts'\n\nenum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n}\n\nconst fruitNativeEnum: = z.nativeEnum(\n Fruit,\n)\n\nconst TreeSchema = z.object({\n fruit: fruitNativeEnum,\n})\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n fruit: unknown\n}\n```\n\nThere are two ways to solve this: provide an identifier to it or resolve all the enums inside `zodToTs()`.\n\nOption 1 - providing an identifier using `withGetType()`:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType, zodToTs } from 'zod-to-ts'\n\nenum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n}\n\nconst fruitNativeEnum = withGetType(\n z.nativeEnum(\n Fruit,\n ),\n // return an identifier that will be used on the enum type\n (ts) => ts.factory.createIdentifier('Fruit'),\n)\n\nconst TreeSchema = z.object({\n fruit: fruitNativeEnum,\n})\n\nconst { node } = zodToTs(TreeSchema)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n fruit: Fruit\n}\n```\n\nOption 2 - resolve enums. This is the same as before, but you just need to pass an option:\n\n```ts\nconst TreeTSType = zodToTs(TreeSchema, { resolveNativeEnums: true })\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n node: {\n fruit: Fruit\n },\n store: {\n nativeEnums: [\n enum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n }\n ]\n }\n}\n```\n\nNote: These are not the actual values, they are TS representation. The actual values are TS AST nodes.\n\nThis option allows you to embed the enums before the schema without actually depending on an external enum type.\n"
"readme": "# zod-to-ts\n\ngenerate TypeScript types from your [Zod](https://github.com/colinhacks/zod) schema\n\n## Installation\n\n```\nnpm install zod-to-ts zod typescript\n```\n\nYou must be on zod@3 and typescript@4.\n\n## Usage\n\n```ts\nimport { z } from 'zod'\nimport { zodToTs } from 'zod-to-ts'\n\n// define your Zod schema\nconst UserSchema = z.object({\n username: z.string(),\n age: z.number(),\n inventory: z.object({\n name: z.string(),\n itemId: z.number(),\n }).array(),\n})\n\n// pass schema and name of type/identifier\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\n```\n\nYou must pass in the identifier `User` or it will default to `Identifier`. This is necessary to handle cases like recursive types and native enums. `zodToTs()` only returns the type value, not the actual type declaration. If you want to add an identifier to the type and create a type declaration, you can use the `createTypeAlias()` utility:\n\n```ts\nimport { createTypeAlias, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst typeAlias = createTypeAlias(node, identifier)\n```\n\nresult:\n\n```ts\ntype User = {\n username: string\n}\n```\n\n`zodToTs()` and `createTypeAlias()` return a TS AST nodes, so if you want to get the node as a string, you can use the `printNode()` utility.\n\n`zodToTs()`:\n\n```ts\nimport { printNode, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst nodeString = printNode(node)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```\n\"{\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\"\n```\n\n`createTypeAlias()`:\n\n```ts\nimport { createTypeAlias, printNode, zodToTs } from 'zod-to-ts'\n\nconst identifier = 'User'\nconst { node } = zodToTs(UserSchema, identifier)\nconst typeAlias = createTypeAlias(node, identifier)\nconst nodeString = printNode(typeAlias)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```\n\"type User = {\n username: string\n age: number\n inventory: {\n name: string\n itemId: number\n }[]\n}\"\n```\n\n## Overriding Types\n\nYou can use `withGetType` to override a type, which is useful when more information is needed to determine the actual type. Unfortunately, this means working with the TS AST:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType, zodToTs } from 'zod-to-ts'\n\nconst DateSchema = withGetType(\n z.instanceof(Date),\n (ts) => ts.factory.createIdentifier('Date'),\n)\n\nconst ItemSchema = z.object({\n name: z.string(),\n date: DateSchema,\n})\n\nconst { node } = zodToTs(ItemSchema, 'Item')\n```\n\nresult without `withGetType` override:\n\n```ts\ntype Item = {\n name: string\n date: any\n}\n```\n\nresult with override:\n\n```ts\ntype Item = {\n name: string\n date: Date\n}\n```\n\n[TypeScript AST Viewer](https://ts-ast-viewer.com/) can help a lot with this if you are having trouble referencing something. It even provides copy-pastable code!\n\n### Special Cases\n\n#### [z.lazy()](https://github.com/colinhacks/zod#recursive-types)\n\nLazy types default to referencing the root type (`User` in the following example). It is impossible to determine what it is referencing otherwise.\n\n```ts\n// Zod cannot infer types when you use the z.lazy\n// so you must define it\nimport { z } from 'zod'\ntype User = {\n username: string\n friends: User[]\n}\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n friends: z.lazy(() => UserSchema).array(),\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n```ts\ntype User = {\n username: string\n friends: User[]\n}\n```\n\nBut what happens when the schema looks like this?\n\n```ts\ntype User = {\n username: string\n item: {\n name: string\n itemId: string\n }\n friends: User[]\n}\n\n// essentially when you are referencing a different field\n// and not the root type\nconst friendItems = z.lazy(() => UserSchema.item).array()\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n item: z.object({\n name: z.string(),\n id: z.number(),\n }),\n friendItems,\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n username: string\n item: {\n name: string\n id: number\n }\n friendItems: User[]\n}\n```\n\n`friendItems` will still have the `User` type even though it is actually referencing `UserSchema[\"item\"]`. You must provide the actual type using `withGetType`:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType } from 'zod-to-ts'\ntype User = {\n username: string\n item: {\n name: string\n id: number\n }\n friends: User[]\n}\n\nconst friendItems: z.Schema<User['item'][]> = withGetType(\n z.lazy(() => UserSchema.item).array(),\n // return a TS AST node\n (ts, identifier) =>\n ts.factory.createIndexedAccessTypeNode(\n ts.factory.createTypeReferenceNode(\n ts.factory.createIdentifier(identifier),\n undefined,\n ),\n ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),\n ),\n)\n\nconst UserSchema: z.ZodSchema<User> = z.object({\n username: z.string(),\n item: z.object({\n name: z.string(),\n id: z.number(),\n }),\n friendItems,\n})\n\nconst { node } = zodToTs(UserSchema, 'User')\n```\n\nresult:\n\n```ts\n{\n username: string\n item: {\n name: string\n id: number\n }\n friendItems: User['item'][]\n}\n```\n\n#### [z.nativeEnum()](https://github.com/colinhacks/zod#native-enums)\n\n`z.enum()` is always preferred, but sometimes `z.nativeEnum()` is necessary. `z.nativeEnum()` works similarly to `z.lazy()` in that the identifier of the enum cannot be determined:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType } from 'zod-to-ts'\n\nenum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n}\n\nconst fruitNativeEnum: = z.nativeEnum(\n Fruit,\n)\n\nconst TreeSchema = z.object({\n fruit: fruitNativeEnum,\n})\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n fruit: unknown\n}\n```\n\nThere are two ways to solve this: provide an identifier to it or resolve all the enums inside `zodToTs()`.\n\nOption 1 - providing an identifier using `withGetType()`:\n\n```ts\nimport { z } from 'zod'\nimport { withGetType, zodToTs } from 'zod-to-ts'\n\nenum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n}\n\nconst fruitNativeEnum = withGetType(\n z.nativeEnum(\n Fruit,\n ),\n // return an identifier that will be used on the enum type\n (ts) => ts.factory.createIdentifier('Fruit'),\n)\n\nconst TreeSchema = z.object({\n fruit: fruitNativeEnum,\n})\n\nconst { node } = zodToTs(TreeSchema)\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n fruit: Fruit\n}\n```\n\nOption 2 - resolve enums. This is the same as before, but you just need to pass an option:\n\n```ts\nconst TreeTSType = zodToTs(TreeSchema, { resolveNativeEnums: true })\n```\n\nresult:\n\n<!-- dprint-ignore -->\n```ts\n{\n node: {\n fruit: Fruit\n },\n store: {\n nativeEnums: [\n enum Fruit {\n Apple = 'apple',\n Banana = 'banana',\n Cantaloupe = 'cantaloupe',\n }\n ]\n }\n}\n```\n\nNote: These are not the actual values, they are TS representation. The actual values are TS AST nodes.\n\nThis option allows you to embed the enums before the schema without actually depending on an external enum type.\n"
}

@@ -122,3 +122,3 @@ # zod-to-ts

import { z } from 'zod'
import { withGetType } from 'zod-to-ts'
import { withGetType, zodToTs } from 'zod-to-ts'

@@ -125,0 +125,0 @@ const DateSchema = withGetType(

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc