question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Recursive type: Type instantiation is excessively deep and possibly infinite.

See original GitHub issue

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:

type V = [V] | "Null"

const V : z.ZodSchema<V> = z.lazy(() =>
  z.union([
    z.tuple([V]),
    z.literal("Null")
]))

However a loosely similar type works fine:

interface V {
  a: [V]
}

const V : z.ZodSchema<V> = z.lazy(() => 
  z.object({
    a: z.tuple([V])
  })
)

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
takahashcommented, Aug 25, 2021

A similar error occurs when TypeScript version is 4.3 or higher.

const C = z.object({
  foo: z.string(),
  bar: z.number().optional(),
})
utils/validations/schema.ts:4:11 - error TS2589: Type instantiation is excessively deep and possibly infinite.
4 const C = z.object({
            ~~~~~~~~~~
5   foo: z.string(),
  ~~~~~~~~~~~~~~~~~~
6   bar: z.number(),
  ~~~~~~~~~~~~~~~~~~
7 })
  ~~

I’m using TypeScrip version 4.1.5 now because I don’t get this error with the version. Next.js recommends TypeScript version 4.3.2 or higher, so if anyone knows how to avoid this error, please let me know.

3reactions
scotttrinhcommented, Aug 27, 2021

@takahash

This sounds similar to an issue where you might have two different versions of zod installed? Are you exporting C into a different project that might have a different version of zod also declared?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am I getting "Type instantiation is excessively deep and ...
The AllModuleActions<M> type is recursive in a way that the compiler cannot ... Type instantiation is excessively deep and possibly infinite.
Read more >
4.6.2 regression: `Type instantiation is excessively deep and ...
Actual behavior ... Type instantiation is excessively deep and possibly infinite. at the ${P}.${DeepKeys<T[P]>} snippet. Expected behavior.
Read more >
Documentation - TypeScript 4.5
TypeScript often needs to gracefully fail when it detects possibly infinite recursion, or any type expansions that can take a long time and...
Read more >
Type instantiation is excessively deep and possibly infinite ...
Getting `Type instantiation is excessively deep and possibly infinite` errors whenever I pass MongoClient as a parameter in my code after ...
Read more >
DefinitelyTyped/DefinitelyTyped - Gitter
It doesn't seem like the team at TS ever uses recursive types. ... the lint tests I still got n Type instantiation is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found