diff --git a/__tests__/type-external.ts b/__tests__/type-external.ts index 56299ad2..fcf3b2df 100644 --- a/__tests__/type-external.ts +++ b/__tests__/type-external.ts @@ -1,5 +1,6 @@ import {isType, JSONArray, JSONObject, JSONTypes} from "type-plus" import {Draft} from "../src/types/types-external" +import {createDraft, current, original} from "../src/immer" describe("Draft", () => { test("can use JSONTypes as T", () => { @@ -17,3 +18,27 @@ describe("Draft", () => { isType.equal() }) }) + +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) + const result = current(draft) + + // Readonly base ensures Draft differs, exposing current()'s typing. + isType.equal() + }) +}) + +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) + const result = original(draft) + + // Readonly base ensures Draft differs, exposing original()'s typing. + isType.equal() + }) +}) diff --git a/src/core/current.ts b/src/core/current.ts index c573ee12..81c431ae 100644 --- a/src/core/current.ts +++ b/src/core/current.ts @@ -1,5 +1,6 @@ import { die, + Draft, isDraft, shallowCopy, each, @@ -11,8 +12,8 @@ import { } 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(value: T): T -export function current(value: any): any { +export function current(value: Draft): T +export function current(value: Draft): any { if (!isDraft(value)) die(10, value) return currentImpl(value) } diff --git a/src/core/immerClass.ts b/src/core/immerClass.ts index b0256d77..a2e8d4c4 100644 --- a/src/core/immerClass.ts +++ b/src/core/immerClass.ts @@ -151,7 +151,7 @@ export class Immer implements ProducersFns { createDraft(base: T): Draft { if (!isDraftable(base)) die(8) - if (isDraft(base)) base = current(base) + if (isDraft(base)) base = current(base as Draft) const scope = enterScope(this) const proxy = createProxy(scope, base, undefined) proxy[DRAFT_STATE].isManual_ = true diff --git a/src/utils/common.ts b/src/utils/common.ts index 2ff01b6b..8afb3d1b 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -2,7 +2,7 @@ import { DRAFT_STATE, DRAFTABLE, Objectish, - Drafted, + Draft, AnyObject, AnyMap, AnySet, @@ -66,8 +66,8 @@ export function isPlainObject(value: any): boolean { /** Get the underlying object that is represented by the given draft */ /*#__PURE__*/ -export function original(value: T): T | undefined -export function original(value: Drafted): any { +export function original(value: Draft): T +export function original(value: Draft): any { if (!isDraft(value)) die(15, value) return value[DRAFT_STATE].base_ }