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

fix: .merge results in Typescript error #57

Closed
wants to merge 2 commits into from

Conversation

roncohen
Copy link

@roncohen roncohen commented Nov 21, 2024

The following code:

import ZodAccelerator from "@duplojs/zod-accelerator";

const schema = zod.object({}).merge(zod.object({}))

caused Type instantiation is excessively deep and possibly infinite.ts(2589):

Screenshot 2024-11-21 at 20 38 01

Note: I was unfortunately unable to understand CONTRIBUTING.md because it's in French. I also was also unable to make the pre-commit hook work:

❯ git commit
file:///Users/ron/Projects/bucket/experiments/zod-accelerator/node_modules/@commitlint/cli/lib/cli.js:132
        throw err;
        ^

Error: ENOENT: no such file or directory, open '/Users/ron/Projects/bucket/experiments/zod-accelerator/.git/COMMIT_EDITMSG'
    at async open (node:internal/fs/promises:639:25)
    at async Object.readFile (node:internal/fs/promises:1249:14)
    at async getEditCommit (file:///Users/ron/Projects/bucket/experiments/zod-accelerator/node_modules/@commitlint/read/lib/get-edit-commit.js:11:22)
    at async main (file:///Users/ron/Projects/bucket/experiments/zod-accelerator/node_modules/@commitlint/cli/lib/cli.js:179:19) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/ron/Projects/bucket/experiments/zod-accelerator/.git/COMMIT_EDITMSG'
}

@roncohen roncohen requested a review from mathcovax as a code owner November 21, 2024 19:43
@mathcovax
Copy link
Contributor

Hello, I will see that when I got some time.

Thanks for your contribution. 🙂

@@ -6,7 +6,7 @@ import { zodSchemaIsAsync } from "./utils/zodSchemaIsAsync";

declare module "zod" {
interface ZodType {
accelerator?: ZodAcceleratorParser<this>;
accelerator?: ZodAcceleratorParser;
Copy link
Contributor

@mathcovax mathcovax Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

declare module "zod" {
	interface ZodType<
		Output = any,
		Def extends zod.ZodTypeDef = zod.ZodTypeDef,
		Input = Output,
	> {
		accelerator?: ZodAcceleratorParser<
                         ZodType<Output, Def, Input>
                 >;
	}
}

This solution seems the best to me, it allows you to save a typing but not have any problems ts(2589).

@@ -180,4 +180,63 @@ describe("object type", () => {
]);
}
});

it("input merged object", () => {
const schema = zod.object({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const schemaA = zod.object({
	test1: zod.string(),
});

const schemaB = zod.object({
	test2: zod.number(),
});

const schema = schemaA.merge(schemaB).strict();

This statement is a little better, it is more readable. (You would tell me on one hand, my tests are not very beautiful ^^')

@mathcovax
Copy link
Contributor

I made the decision to apply the fix to avoid dragging things out. Here's the PR :#60 , thanks again for your contribution!

@mathcovax mathcovax closed this Nov 29, 2024
@roncohen
Copy link
Author

thats great, thanks!

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.

2 participants