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

Type instantiation is excessively deep and possibly infinite in version 4.1 #41406

Closed
mateja176 opened this issue Nov 4, 2020 · 11 comments
Closed
Labels
Needs Investigation This issue needs a team member to investigate its status.
Milestone

Comments

@mateja176
Copy link

Motivation
I wrote a type to make [lodash get[(https://lodash.com/docs/4.17.15#get) and especially formik getFieldProps type safe by writing a generic type which is represent a union of string literal types representing a full . separated path to leaf nodes of a possibly nested record.

TypeScript Version: 4.1.0

Search Terms:

  • Type instantiation is excessively deep and possibly infinite.ts(2589)
  • Mapped types
  • Generics
  • Conditional types

Code

// @ts-ignore
type FieldKey<O extends Record<string, unknown>> = { [key in keyof O]: O[key] extends Record<string, unknown> ? `${string & key}.${string & FieldKey<O[key]>}` : key }[keyof O];
type Example = { dog: "Jacky", toys: { mouse: { color: "gray", teeth: "white" }, bone: { color: 'blue', pimples: true }, box: { cube: { ball: 'red' } } } };
type FieldKey2 = FieldKey<Example>;

Expected behavior:

No error is thrown.

Actual behavior:

Type instantiation is excessively deep and possibly infinite.ts(2589) error is thrown.

Playground Link:

This is a link to the playground.

Related Issues:

@paales
Copy link

paales commented Nov 8, 2020

Experiencing the same with a similar approach. I believe the error wasn't there with typescript 4.1.0-beta, but started to occur with 4.1.1-rc

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Nov 12, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 12, 2020
@pedrodurek
Copy link

pedrodurek commented Dec 11, 2020

Hey guys, I'm facing a similar issue, but it seems to be more like a typescript limitation (despite the data size not being that large).
You can see on the examples below that when I'm handling 22 namespaces (ns), it doesn't throw any error, but from 23 namespaces (ns) it does. It infers the proper type though.

We're relying on those types here, so I cannot simply use @ts-ignore to suppress the error.
It'd also be great to have a flag to suppress this error and/or control the depth under tsconfig.

@bsssshhhhhhh
Copy link

Currently running into this error on 4.1.2 trying to improve the types in my library. Increasing the constraintDepth limit in getImmediateBaseConstraint() makes the error disappear.

+1 for turning this into a tsconfig setting instead of hardcoding it like this - it seems to be an arbitrary limit rather than a technical one.

@ClickerMonkey
Copy link

My code base is also plagued by this error, sometimes it goes away for a few seconds, sometimes the exact line the error is being thrown changes, even when the code is not modified. It's interestingly inconsistent!

namjul pushed a commit to namjul/gumption-ui that referenced this issue Apr 12, 2021
This reverts commit 1533de7.

It resulted into the following error:
`Type instantiation is excessively deep and possibly infinite.ts(2589) error is thrown.`
which has been issued here: microsoft/TypeScript#41406

Going to remove this until its clear what the cause is.
anthony-murphy added a commit to microsoft/FluidFramework that referenced this issue Apr 22, 2021
I'd optimistically increased the number of event overloads, but not consumed it. As part of bumping all packages it turns out we've hit an intrinsic typescript limit for recursive types, which was leading to “TS2589: Type instantiation is excessively deep and possibly infinite” . This change reduces the number of overloads to the number which works, and is still more than we currently use anywhere.

There are few typescript issues this could be related to:
microsoft/TypeScript#34933
microsoft/TypeScript#41406
@ahejlsberg
Copy link
Member

This issue no longer reproduces. Closing.

@zilijonas
Copy link

zilijonas commented Jul 22, 2021

Still getting this error with the 4.3.5 version.

@pedrodurek
Copy link

Same ☝️

@Walraz
Copy link

Walraz commented Jul 27, 2021

Same error still in 4.3.5

@paales
Copy link

paales commented Jul 29, 2021

Might be worthwhile to create a testcase in the playground https://www.typescriptlang.org/play?ts=4.4.0-beta and create a new issue

@pedrodurek
Copy link

For my case, the issue was solved on 4.5.0-beta.

@GR34SE
Copy link

GR34SE commented Oct 29, 2021

Can confirm that 4.5.0-beta resolves the issue. For me, it was throwing when complex mobx-state-tree models were passed to generic types such as Instance: Instance<typeof ComplexModel>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

10 participants