valibot: Type instantiation is excessively deep and possibly infinite.

Hello and first of all, thank you for your work with this library. I was eager to migrate from zod to valibot after encountering this error on the use of schemas with many union and transform but the same one seems to occur. Is there maybe a good practice to avoid it? Thank you

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (11 by maintainers)

Most upvoted comments

The way it was resolved in kysely:

Thank you for the tip. I will try to investigate this in the next few weeks.

I am toying with the idea of removing merge because of this TypeScript problem. Meanwhile, Valibot has a workaround that accomplishes exactly the same thing with a good DX. Please give me feedback.

import * as v from 'valibot';

const ObjectSchema1 = v.object({ key1: v.string() });

const ObjectSchema2 = v.object({ key2: v.number() });

const MergedSchema1 = v.merge([ObjectSchema1, ObjectSchema2]);

// This is the workaround that accomplishes exactly the same thing
const MergedSchema2 = v.object({
  ...ObjectSchema1.entries,
  ...ObjectSchema2.entries,
});

Here is a guide that help to choose between intersect and merge: https://valibot.dev/guides/intersections/

Yes, that works too. Thank you! Another workaround that works in most cases is intersection:

import { intersection, object, string } from 'valibot';

const Object1 = object({ key1: string() });
const Object2 = object({ key2: string() });
const Object3 = intersection([Object1, Object2]);

Can you send me some code examples?