immer icon indicating copy to clipboard operation
immer copied to clipboard

TypeScript - Recursive types: Type instantiation is excessively deep and possibly infinite - ts(2589)

Open peterjuras opened this issue 4 years ago • 5 comments

🐛 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)

immer-example

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

Codesandbox 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)

peterjuras avatar Aug 07 '21 07:08 peterjuras

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.

denexapp avatar May 22 '22 22:05 denexapp

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.

unional avatar Oct 30 '22 18:10 unional

btw, I have fork and released the fix of this in https://github.com/unional/immer, as the PR are not moving.

unional avatar Dec 29 '22 09:12 unional

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!
    

elijahbenizzy avatar Jan 02 '23 04:01 elijahbenizzy

:tada: This issue has been resolved in version 9.0.18 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Jan 15 '23 17:01 github-actions[bot]