Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support standard built-in objects #122

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

NexZhu
Copy link
Contributor

@NexZhu NexZhu commented Mar 15, 2023

Why

Browser and Node.js both provide many standard built-in objects such as Error Int8Array BigInt etc.

Before this PR, if types are using these built-in objects, ts-to-zod will fail with missing dependencies, this PR support built-in objects by using Zod's z.instanceof method.

For example:

export interface Person {
  photo?: Uint8Array | null;
}

will generate:

const uint8ArraySchema = z.instanceof(Uint8Array);

export const personSchema = z.object({
  photo: uint8ArraySchema.optional().nullable(),
});

The list of standard built-in objects is from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects

@NexZhu
Copy link
Contributor Author

NexZhu commented Mar 15, 2023

@fabien0102 Please check this out too when you have time, so I can open further improvement PRs after this get merged, thanks! 😄

@fabien0102
Copy link
Owner

Sorry, I totally missed this PR 🫣 I will have a look next week! Nice job 👌

@schiller-manuel
Copy link
Collaborator

can you rebase this so this can get merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants