Skip to content
Myr

Be explicit about not supporting arbitrary properties on arrays

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 4b9a7d3 · Browse files · 4 files+55 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+55 added−2 removed

  • tests:+31 added−0 removed
  • other:+24 added−2 removed

4 files+55 added−2 removed

modified:__tests__/base.js+31 added
569 unmodified lines
570 listener571 )572 }).toThrowError(/not support/)573 })574575 it("should not be possible to add properties to arrays", () => {576 expect(() => {577 produce([], d => {578 d.x = 3579 })580 }).toThrow(581 "Immer does not support setting non-numeric properties on arrays"582 )583 })584585 it("should not be possible to remove properties from arrays", () => {586 expect(() => {587 const base = []588 base.x = 7589 produce(base, d => {590 delete d.x591 })592 }).toThrow(593 "Immer does not support deleting properties from arrays"594 )595 })596 }597598 it("non-numeric array properties will be lost", () => {599 const base = []600 base.x = 7601 const next = produce(base, d => {602 d.push(3)603 })604 expect(next.x).toBe(undefined)605 })606607 it("should not throw error, see #53 - 1", () => {608 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}609 const result = produce(610 base,
675 unmodified lines
modified:src/proxy.js+15 added
modified:changelog.md+5 added
1# Changelog23### 1.6.045* Introduced the `original(draft)` function, that, given an object from the draft, returns the original object from the base state. This can be useful if you need to do reference equality checks, or comparisons with the base state. See [#179](https://github.com/mweststrate/immer/pull/179) by [@RichieAHB](https://github.com/RichieAHB).6* Writing or removing non-numeric properties on arrays is now strictly forbidden (Immer didn't throw before, but correct behavior wasn't guaranteed either)78### 1.5.0910* Added support for patches, through [#168](https://github.com/mweststrate/immer/pull/168). See the [patches](https://github.com/mweststrate/immer#patches) section for details11
165 unmodified lines
modified:readme.md+4 added-2 removed
383 unmodified lines
384```385386## Inline shortcuts using `void`387388Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with. 388Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.389390In such cases you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.391392```javascript
3 unmodified lines
396// Multiple mutations397produce(draft => void (draft.user.age += 1, draft.user.height = 186))398```399400Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft = { draft.user.age += 1}` to avoid cognitive overhead.401402## TypeScript or Flow403404The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.405
43 unmodified lines
449450## Limitations / pitfalls4514521. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).4511. Immer only supports plain objects and arrays.4531. Immer only supports plain objects and arrays. For arrays it is assumed only numeric properties (and length) are used.4541. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice and there should be no circular references.4551. Class instances are not, and will not be supported. Read here more on why that would be a [confusing, conceptual mismatch]4561. Immer only processes native arrays and plain objects (with a prototype of `null` or `Object`). Any other type of value will be treated verbatim! So if you modify a `Map` or `Buffer` (or whatever complex object from the draft state), the changes will be persisted. But, both in your new and old state! So, in such cases, make sure to always produce fresh instances if you want to keep your state truly immutable.4571. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.
166 unmodified lines