Skip to content

Commit

Permalink
feat: withGetTypes utility
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinraja committed Jan 10, 2022
1 parent e46eba0 commit 2d1fe5e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 99 deletions.
52 changes: 25 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ result:

```ts
import { z } from 'zod'
import { GetType } from 'zod-to-ts'
import { withGetType } from 'zod-to-ts'
type User = {
username: string
item: {
Expand All @@ -201,19 +201,18 @@ type User = {
friends: User[]
}

const friendItems: z.Schema<User['item'][]> & GetType = z.lazy(() =>
UserSchema.item
).array()
// you must define the `getType` function property on the schema
// return a TS AST node
friendItems.getType = (ts, identifier) =>
ts.factory.createIndexedAccessTypeNode(
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(identifier),
undefined,
const friendItems: z.Schema<User['item'][]> = withGetType(
z.lazy(() => UserSchema.item).array(),
// return a TS AST node
(ts, identifier) =>
ts.factory.createIndexedAccessTypeNode(
ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(identifier),
undefined,
),
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),
),
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral('item')),
)
)

const UserSchema: z.ZodSchema<User> = z.object({
username: z.string(),
Expand Down Expand Up @@ -244,21 +243,19 @@ result:

#### [z.nativeEnum()](https://github.com/colinhacks/zod#native-enums)

`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. There are two ways to solve this: provide an identifier to it or resolve all the enums inside `zodToTs()`.

Option 1 - providing an identifier:
`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:

```ts
import { z } from 'zod'
import { GetType } from 'zod-to-ts'
import { withGetType } from 'zod-to-ts'

enum Fruit {
Apple = 'apple',
Banana = 'banana',
Cantaloupe = 'cantaloupe',
}

const fruitNativeEnum: z.ZodNativeEnum<typeof Fruit> & GetType = z.nativeEnum(
const fruitNativeEnum: = z.nativeEnum(
Fruit,
)

Expand All @@ -276,27 +273,28 @@ result:
}
```

To fix this, define `getType()`:
There are two ways to solve this: provide an identifier to it or resolve all the enums inside `zodToTs()`.

Option 1 - providing an identifier using `withGetType()`:

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

enum Fruit {
Apple = 'apple',
Banana = 'banana',
Cantaloupe = 'cantaloupe',
}

const fruitNativeEnum: z.ZodNativeEnum<typeof Fruit> & GetType = z.nativeEnum(
Fruit,
const fruitNativeEnum = withGetType(
z.nativeEnum(
Fruit,
),
// return an identifier that will be used on the enum type
(ts) => ts.factory.createIdentifier('Fruit'),
)

// return an identifier that will be used on the enum type
fruitNativeEnum.getType = (ts) => {
return ts.factory.createIdentifier('Fruit')
}

const TreeSchema = z.object({
fruit: fruitNativeEnum,
})
Expand Down
Loading

0 comments on commit 2d1fe5e

Please sign in to comment.