Skip to content

Commit

Permalink
fix: consistent naming for castDraft, fixup 2d27f33
Browse files Browse the repository at this point in the history
denis-sokolov committed Feb 10, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ab3b8d0 commit 70b44a7
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions __tests__/draft.ts
Original file line number Diff line number Diff line change
@@ -304,7 +304,7 @@ test("draft.ts", () => {
expect(true).toBe(true)
})

test("asDraft", () => {
test("castDraft", () => {
type Todo = {readonly done: boolean}

type State = {
@@ -326,7 +326,7 @@ test("#505 original", () => {
})
})

test("asDraft preserves a value", () => {
test("castDraft preserves a value", () => {
const x = {}
expect(castDraft(x)).toBe(x)
})
2 changes: 1 addition & 1 deletion docs/typescript.md
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ This will generate the error:
The type 'readonly Todo[]' is 'readonly' and cannot be assigned to the mutable type '{ done: boolean; }[]'
```

The reason for this error is that we assign our read only, immutable array to our draft, which expects a mutable type, with methods like `.push` etc etc. As far as TS is concerned, those are not exposed from our original `State`. To hint TypeScript that we want to upcast the collection here to a mutable array for draft purposes, we can use the utility `asDraft`:
The reason for this error is that we assign our read only, immutable array to our draft, which expects a mutable type, with methods like `.push` etc etc. As far as TS is concerned, those are not exposed from our original `State`. To hint TypeScript that we want to upcast the collection here to a mutable array for draft purposes, we can use the utility `castDraft`:

`draft.finishedTodos = castDraft(state.unfinishedTodos)` will make the error disappear.

0 comments on commit 70b44a7

Please sign in to comment.