Open tmtron opened 3 years ago
Bump. This is an issue when using the immer
library in particular. I am getting
TypeError: Cannot add property __type, object is not extensible
If I add a placeholder __type
field to my class, I run into the same issue @tmtron encountered with
TypeError: Cannot assign to read only property '__type'
I have the same problem using a discriminator in the @Type
decorator on freezed instances.
It seems to be, that instanceToPlain
has a side-effect, modifying the instance.
My expectation is, that the discriminator property is in the plain object, but the call will never every modify the instance.
Ran into this during a refactor. Vite didn't like the third party NPM package with the fix, and the official package isn't patch-package
friendly. I ended up working around the issue by using a draft function and calling instanceToPlain()
on the draft:
produce(item, (draft) => {
json = JSON.stringify(instanceToPlain(draft));
});
It feels... wrong, but seems to work fine. Because the draft allows writing, we don't get an error at runtime. The side effects are contained within the draft instance, and we discard the resulting object. Would be better if instanceToPlain()
didn't modify the source object though.
Description
When we use immutable helper libraries, like immer, the discriminator property on the instance may be frozen.
In this case
instanceToPlain
(in class-transformer 0.4.0) fails with:Minimal code-snippet showcasing the problem
Expected behavior
Converting to plain should not fail (even for frozen instances).
Actual behavior
instanceToPlain
(in class-transformer 0.4.0) fails with: