You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
immer allows modification of readonly properties of nested class instances that are not marked as immerable. This seems dangerous to me. Just like immer does not allow direct modification of class instances that are not marked as immerable, an exception should be thrown. Even better, the types should not allow this.
import{immerable,produce}from"immer";classAddress{constructor(publicreadonlystreetName: string){this.streetName=streetName;}}classImmerableAddress{[immerable]=true;constructor(publicreadonlystreetName: string){this.streetName=streetName;}}classImmerablePerson{[immerable]=true;constructor(publicreadonlyaddress: Address){this.address=address;}}describe("immer",()=>{constimmerableAddress=newImmerableAddress("foo");it("works fine with immerable nested class instance",()=>{constproduced=produce(newImmerablePerson(immerableAddress),(draft)=>{draft.address.streetName="bar";});expect(produced.address.streetName).toBe("bar");expect(produced.address).toBeInstanceOf(ImmerableAddress);expect(produced.address).not.toBe(immerableAddress);});constaddress=newAddress("foo");it("allows illegal modification of non-immerable nested class instance",()=>{constproduced=produce(newImmerablePerson(address),(draft)=>{draft.address.streetName="bar";// works unexpectedly});expect(produced.address.streetName).toBe("bar");expect(produced.address).toBeInstanceOf(Address);expect(produced.address).toBe(address);});});
🐛 Bug Report
immer allows modification of
readonly
properties of nested class instances that are not marked asimmerable
. This seems dangerous to me. Just like immer does not allow direct modification of class instances that are not marked asimmerable
, an exception should be thrown. Even better, the types should not allow this.Link to repro
CodeSandbox demo
Environment
Immer 10.1.1
The text was updated successfully, but these errors were encountered: