superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Immer compatibility

Open nhagen opened this issue 4 years ago • 1 comments

We use immer, and in some cases we're having trouble with this line

https://github.com/ianstormtaylor/superstruct/blob/c3ca5e4a3b7cfc24658f32c90fec25c671ed308f/src/utils.ts#L178

which causes

TypeError: Cannot assign to read only property 'foo' of object '#<Object>'

If the result of an immer produce function is passed to create().

I spent a little bit trying to come up with a simplest possible reproduction, but actually just confirmed that this should work. But just from reading the code, its trying to mutate the value thats passed in, so that doesn't make sense.

Should this work? Should this not work? Here is a repl.it with an example. My hunch is that there is some more compex object that I can pass in which would reproduce my issue but I haven't found it yet.

https://replit.com/@zxcvnathan/ImmerSuperstruct#index.js

nhagen avatar May 07 '21 02:05 nhagen

For what it's worth to anybody running into this, we're just making sure that we destroy any immer wrappers when data is being called out to helper functions, like so.

JSON.parse(JSON.stringify(draft))

illepic avatar Mar 21 '25 21:03 illepic