modular-forms icon indicating copy to clipboard operation
modular-forms copied to clipboard

Type instantiation is excessively deep and possibly infinite

Open juanmarin-co opened this issue 1 year ago • 11 comments

Hi! I'm using [email protected] and @modular-forms/[email protected].

I just bootstrapped a new application trying to find why typescript compilation was taking much longer for one of my projects.

For this simple setup:

import { formAction$, zodForm$ } from "@modular-forms/qwik";
import { z } from "zod";

const userInfoSchema = z.object({
  fullName: z.string(),
  phone: z.string(),
  company: z.string(),
});

type UserInfoForm = z.input<typeof userInfoSchema>;

export const useFormAction = formAction$<UserInfoForm>(async (userInfo) => {
  console.log(userInfo);
}, zodForm$(userInfoSchema));

I get the following error:

src/routes/index.tsx:19:4 - error TS2589: Type instantiation is excessively deep and possibly infinite.

21 }, zodForm$(userInfoSchema));
      ~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/routes/index.tsx:21

juanmarin-co avatar Oct 08 '24 14:10 juanmarin-co

Thanks for creating this issue. Do you know the last version of Modular Forms where this worked without a TS error?

fabian-hiller avatar Oct 08 '24 19:10 fabian-hiller

@fabian-hiller It’s strange; the issue doesn’t appear to be tied to the version of modular-forms. Even after downgrading modular-forms in a newly initialized project, the problem persists.

juanmarin-co avatar Oct 09 '24 13:10 juanmarin-co

Is it related to your TS version?

fabian-hiller avatar Oct 09 '24 17:10 fabian-hiller

At this point I'm not sure what's causing the problem. I've also tried multiple ts versions and the issue persists.

Here's a reproduction repo so you can try it. It's just a fresh qwik repo with a very simple schema.

https://github.com/juanpmarin/modular-forms-ts-repro

You can reproduce the error by running pnpm exec tsc

juanmarin-co avatar Oct 10 '24 15:10 juanmarin-co

I can't reproduce the bug. It seems to work as expected on my machine. 😬

fabian-hiller avatar Oct 10 '24 20:10 fabian-hiller

@fabian-hiller what node version are you using? Did you use pnpm? I'm using node v20.17.0

juanmarin-co avatar Oct 10 '24 20:10 juanmarin-co

I just found something interesting, it seems that qwik-city re-exports zod, and if you import z from qwik city (because the editor suggests it), the issue appears.

https://github.com/juanpmarin/modular-forms-ts-repro/commit/248d61aba0a8819bad8bff6390e595459a104de2#diff-3d4d2177c21702138c6c0473b20d38d819dec2249dd99cbf1d00fde1f6ac5115

Can you try to reproduce it now?

juanmarin-co avatar Oct 11 '24 02:10 juanmarin-co

Yes, with import { z } from "@builder.io/qwik-city" the same error happens to me.

fabian-hiller avatar Oct 11 '24 15:10 fabian-hiller

It was a hard one to find and started to happen in newer versions of qwik, do you think is there a way to warn users that pass the wrong instance of zod?

juanmarin-co avatar Oct 11 '24 22:10 juanmarin-co

I think Qwik should remove the z export and devs should import it directly from Zod.

fabian-hiller avatar Oct 12 '24 16:10 fabian-hiller