-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Recursive type: Type instantiation is excessively deep and possibly infinite. #577
Comments
This works if you do this: const V: z.ZodSchema<V, z.ZodTypeDef, any> = z.lazy(() =>
z.union([z.tuple([V]), z.literal("Null")])
); The third generic parameter to |
An equivalence check gone wild? Thanks for the easy workaround! |
A similar error occurs when TypeScript version is 4.3 or higher.
I'm using TypeScrip version 4.1.5 now because I don't get this error with the version. |
@takahash do you have a reproduction repo? |
@takahash What version of Zod were you using with Typescript 4.3.2? In my case I experienced the same issue with tsdx because internally they were using a very old version of typescript. I haven't tried with NextJS yet. |
I'm sorry, when I tried a little more, it seemed that the cause was that the zod was read in different files.
import { z } from 'zod'
export const C = z.object({
foo: z.string(),
bar: z.number().optional(),
})
import { z } from 'zod'
type Value = { _errors: string[] }
export const handleValidate =
(schema: z.ZodObject<any>) => (values: unknown) => {
let formattedErrors: Record<string, string> = {}
try {
schema.parse(values)
} catch (error) {
if (error instanceof z.ZodError) {
const errors = error.format()
Object.entries(errors).forEach(([key, value]: [string, Value]) => {
if (value._errors && value._errors.length) {
formattedErrors[key] = value._errors[0]
}
})
return formattedErrors
}
}
} When I put the above two files together, the error disappeared even if the TypeScript version is 4.3.2 or higher. |
This sounds similar to an issue where you might have two different versions of |
I saw this in my vscode hints but nowhere in my actual builds. Turns out I had to uninstall the ts-next (Microsoft TypeScript Nightly) extension from vsc. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I'm running into a problem creating a parser for a fairly simple recursive type. The following errors with
Type instantiation is excessively deep and possibly infinite. TS2589
:However a loosely similar type works fine:
The use of the Tuple is significant, using an array works fine for both.
I'd be willing to work on a pr but I had a poke around and didn't get far in finding out what causes this, if you have any tips on what might be causing it I can take another look.
The text was updated successfully, but these errors were encountered: