MetaMask / utils

Various JavaScript / TypeScript utilities of wide relevance to the MetaMask codebase.
https://metamask.github.io/utils/index.html
ISC License
31 stars 10 forks source link

An object type that contains `Json` causes a recursive error in conjunction with `Draft`/`WritableDraft` types from Immer #168

Open mcmire opened 9 months ago

mcmire commented 9 months ago

If you have an object type such as:

{
  foo: Json
}

and you use it to change state within an Immer produce callback — such the callback that update in BaseController v2 takes — then you will get an error:

Type instantiation is excessively deep and possibly infinite

This is happening because Json is a recursive type and Draft and WritableDraft trampoline as they recurse through that Json type. There's an open bug report on the Immer issue tracker with a proposed fix that was eventually reverted.

We should work out some way to avoid this.

MajorLift commented 9 months ago

Some starting points for tackling the "Type instantiation is excessively deep" error:

This might not be enough to resolve the issue if the mutual recursion depth is infinite even with tail call elimination.

MajorLift commented 8 months ago

Might be resolved by TypeScript 5.5: https://github.com/microsoft/TypeScript/pull/57293

mcmire commented 1 week ago

I checked the Immer repo and it looks like this problem still exists: https://github.com/immerjs/immer/issues/839