fix: update current and original typings to assert draft value is unwrapped (#1236)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Dean Kerr · committed by GitHub · parent 7390d6c · Browse files · 4 files·+32 added−6 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+32 added−6 removed
- tests:+25 added−0 removed
- other:+7 added−6 removed
Provenance
- Commit
bf2d154- Tree
444a473- Author
Dean Kerr <oriondean@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7390d6c
4 files changed+32 added−6 removed
modified:__tests__/type-external.ts+25 added
12345679 unmodified lines17181920212223242526272829303132333435363738394041424344import {isType, JSONArray, JSONObject, JSONTypes} from "type-plus"import {Draft} from "../src/types/types-external"import {createDraft, current, original} from "../src/immer"
describe("Draft<T>", () => { test("can use JSONTypes as T", () => { type A = Draft<JSONTypes>9 unmodified lines type A = Draft<[string, number, JSONArray, JSONObject]> isType.equal<true, [string, number, JSONArray, JSONObject], A>() })})
describe("current() typings", () => { test("returns non-draft type from a draft input", () => { type Base = Readonly<{a: boolean}> const base: Base = {a: true} const draft = createDraft<Base>(base) const result = current(draft)
// Readonly base ensures Draft<Base> differs, exposing current()'s typing. isType.equal<true, Base, typeof result>() })})
describe("original() typings", () => { test("returns non-draft type from a draft input", () => { type Base = Readonly<{a: boolean}> const base: Base = {a: true} const draft = createDraft<Base>(base) const result = original(draft)
// Readonly base ensures Draft<Base> differs, exposing original()'s typing. isType.equal<true, Base, typeof result>() })})modified:src/core/current.ts+3 added-2 removed
12345673 unmodified lines11121314141515161718192027 unmodified linesimport { die, Draft, isDraft, shallowCopy, each, DRAFT_STATE,3 unmodified lines isFrozen} from "../internal"
/** Takes a snapshot of the current state of a draft and finalizes it (but without freezing). This is a great utility to print the current state during debugging (no Proxies in the way). The output of current can also be safely leaked outside the producer. */export function current<T>(value: T): Texport function current(value: any): any {export function current<T>(value: Draft<T>): Texport function current(value: Draft<any>): any { if (!isDraft(value)) die(10, value) return currentImpl(value)}
27 unmodified linesmodified:src/core/immerClass.ts+1 added-1 removed
149 unmodified lines150 }151 152 createDraft<T extends Objectish>(base: T): Draft<T> {153 if (!isDraftable(base)) die(8)154- if (isDraft(base)) base = current(base)154+ if (isDraft(base)) base = current(base as Draft<T>)155 const scope = enterScope(this)156 const proxy = createProxy(scope, base, undefined)157 proxy[DRAFT_STATE].isManual_ = true158 leaveScope(scope)115 unmodified linesmodified:src/utils/common.ts+3 added-3 removed
1 import {2 DRAFT_STATE,3 DRAFTABLE,4 Objectish,5- Drafted,5+ Draft,6 AnyObject,7 AnyMap,8 AnySet,9 ImmerState,55 unmodified lines65 }66 67 /** Get the underlying object that is represented by the given draft */68 /*#__PURE__*/69-export function original<T>(value: T): T | undefined70-export function original(value: Drafted<any>): any {69+export function original<T>(value: Draft<T>): T70+export function original(value: Draft<any>): any {71 if (!isDraft(value)) die(15, value)72 return value[DRAFT_STATE].base_73 }74 212 unmodified lines