State libraries compare old and new state by reference: if state.todos is the same object as before, nothing in the todo list re-renders. That needs immutable updates, a new object for everything on the path to a change and the old objects everywhere else. Written by hand, a change three levels deep is a pile of spreads. A draft fixes that: the code changes a stand-in for the state as if it were mutable, and the library builds the immutable result from the changes.
base is never changed, at any depth (the tests freeze it).
The result has every change the recipe made. Every object on the path to a change is a new object; every other object is the very same object as in base (===), including siblings of changed objects and unchanged objects inside changed ones.
When nothing changed, the result is base itself. Reading is not a change, and assigning a value that is Object.is-equal to the current one is not a change.
Inside the recipe, the draft behaves like the data: nested plain objects and arrays are drafts too, in, Object.keys and JSON.stringify see the changes made so far, Array.isArray is true for arrays, and the built-in array methods work.
A draft placed somewhere else, directly (draft.backup = draft.meta) or inside a new object, ends up in the result as its final value. A draft met twice gives the same object twice. A new object that the recipe froze cannot hold a draft: produce throws a TypeError instead.
New objects that the recipe assigns are kept as they are. Values that are not plain objects or arrays (Date, Map, class instances) are not drafted.
Copies keep the prototype of the object they copy (a null-prototype object stays one) and all its own enumerable properties: symbol keys too, and an array's extra properties besides its items.
A recipe that returns a value other than undefined makes that value the result (drafts inside it become their final values). Returning the draft itself is the same as returning nothing. Changing the draft and returning a different value throws an Error.
After produce returns or throws, every draft stops working: any use of one throws a TypeError. An error thrown by the recipe comes out of produce unchanged.
produce throws a TypeError when base is not a plain object or array, or recipe is not a function.