Skip to content
Myr

Add test to verify solution for #381

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 3711dec · Browse files · 2 files+37 added−1 removed
This commit

State

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

Diff stats

2 files+37 added−1 removed

  • tests:+17 added−1 removed
  • other:+20 added−0 removed

2 files+37 added−1 removed

modified:__tests__/immutable.ts+17 added-1 removed
modified:docs/typescript.md+20 added
97 unmodified lines
98`draft.finishedTodos = castDraft(state.unfinishedTodos)` will make the error disappear.99100There is also the utility `castImmutable`, in case you ever need to achieve the opposite. Note that these utilities are for all practical purposes no-ops, they will just return their original value.101102Tip: You can combine `castImmutable` with `produce` to type the return type of `produce` as something immutable, even when the original state was mutable:103104```typescript105// a mutable data structure106const baseState = {107 todos: [{108 done: false109 }]110}111112const nextState = castImmutable(produce(baseState, _draft => {}))113114// inferred type of nextState is now:115{116 readonly todos: ReadonlyArray<{117 readonly done: boolean118 }>119})120```121122## Compatibility123124**Note:** Immer v5.3+ supports TypeScript v3.7+ only.125
5 unmodified lines