TypeScript - Recursive types: Type instantiation is excessively deep and possibly infinite - ts(2589)
🐛 Bug Report
I'm using immer in a TypeScript project together with the type-fest library. type-fest got updated to 2.0.0 recently and since then the produce method throws the following TypeScript compiler error when used with a JsonObject type from type-fest:
Type instantiation is excessively deep and possibly infinite - ts(2589)

The error appears as soon as the draft variable is added to the produce method. There have been changes to the JsonObject type in type-fest, but it doesn't seem that a lot has changed in the type structure. These are the changes between the 1.4.0 and 2.0.0
Potentially related to #347.
Link to repro
To Reproduce
Open the Codesandbox
Observed behavior
TypeScript throws a compilation error:
Type instantiation is excessively deep and possibly infinite - ts(2589)
Expected behavior
TypeScript should not throw any error
Environment
We only accept bug reports against the latest Immer version.
- Immer version: 9.0.5
- TypeScript version: 4.3.5
- [X] I filed this report against the latest version of Immer
- [ ] Occurs with
setUseProxies(true) - [ ] Occurs with
setUseProxies(false)(ES5 only)
I also have this problem. We use recursive types for app state, and we use the @reduxjs/toolkit package to manage state, which uses immer under the hood.
I've the same problem with type-plus.
The issue is that the JSONTypes are recursive in nature, so as the Draft<T> tries to resolve a recursive type recursively, it becomes an infinite loop.
btw, I have fork and released the fix of this in https://github.com/unional/immer, as the PR are not moving.
OK, I have this as well. Really glad to see that others are hitting this too and I'm not crazy 😆
Posting this here for googleability -- do let me know if there's something I can do to help.
Within a nested type I had:
type JSONValue =
| string
| number
| boolean
| { [x: string]: {} }
| Array<JSONValue>;```
Which ended up giving me that message. For now I cut out the JSONValue, but happy to look at PRs or do anything else I can do to help!
:tada: This issue has been resolved in version 9.0.18 :tada:
The release is available on:
Your semantic-release bot :package::rocket: