Skip to content
Myr

Introduced `castDraft` / `castImmutable`, implements #486

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 4204af5 · Browse files · 4 files+81 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+81 added−2 removed

  • tests:+16 added−1 removed
  • other:+65 added−1 removed

4 files+81 added−2 removed

modified:__tests__/draft.ts+16 added-1 removed
1import {assert, _} from "spec.ts"2import {Draft} from "../src/index"2import produce, {Draft, Draft, castDraft} from "../src/index"34// For checking if a type is assignable to its draft type (and vice versa)5const toDraft: <T>(value: T) => Draft<T> = x => x as any6const fromDraft: <T>(draft: Draft<T>) => T = x => x as any
295 unmodified lines
302 }303304 expect(true).toBe(true)305})306307test("asDraft", () => {308 type Todo = {readonly done: boolean}309310 type State = {311 readonly finishedTodos: ReadonlyArray<Todo>312 readonly unfinishedTodos: ReadonlyArray<Todo>313 }314315 function markAllFinished(state: State) {316 produce(state, draft => {317 draft.finishedTodos = castDraft(state.unfinishedTodos)318 })319 }320})
modified:docs/api.md+2 added
modified:docs/typescript.md+37 added
modified:src/index.ts+26 added-1 removed
1import {IProduce, IProduceWithPatches, Immer} from "./internal"1import {2 IProduce,3 IProduceWithPatches,4 Immer,5 Draft,6 Immutable7} from "./internal"89export {10 Draft,11 Immutable,
75 unmodified lines
87 * changes that were made.88 */89export const finishDraft = immer.finishDraft.bind(immer)9091/**92 * This function is actually a no-op, but can be used to cast an immutable type93 * to an draft type and make TypeScript happy94 *95 * @param value96 */97export function castDraft<T>(value: T): Draft<T> {98 return value as any99}100101/**102 * This function is actually a no-op, but can be used to cast a mutable type103 * to an immutable type and make TypeScript happy104 * @param value105 */106export function castImmutable<T>(value: T): Immutable<T> {107 return value as any108}109110export {Immer}