types: async produce
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent 13b0ed0 · Browse files · 2 files·+49 added−14 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+49 added−14 removed
- tests:+42 added−5 removed
- other:+7 added−9 removed
Provenance
- Commit
c6cd632- Tree
9e3369e- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
13b0ed0
2 files changed+49 added−14 removed
modified:__tests__/produce.ts+42 added-5 removed
194 unmodified lines195 })196 exactType(result, {} as any[])197 })198 199-it("can produce nothing", () => {200- let result = produce({}, _ => nothing)199+it("can produce an undefined value", () => {200+ let base = {} as {readonly a: number}201+202+ // Return only nothing.203+ let result = produce(base, _ => nothing)204 exactType(result, undefined)205+206+ // Return maybe nothing.207+ let result2 = produce(base, draft => {208+ if (draft.a > 0) return nothing209+ })210+ exactType(result2, {} as typeof base | undefined)211 })212 213+it("can return the draft itself", () => {214+ let base = {} as {readonly a: number}215+ let result = produce(base, draft => draft)216+217+ // Currently, the `readonly` modifier is lost.218+ exactType(result, {} as {a: number} | undefined)219+})220+221+it("can return a promise", () => {222+ let base = {} as {readonly a: number}223+224+ // Return a promise only.225+ exactType(226+ produce(base, draft => {227+ return Promise.resolve(draft.a > 0 ? null : undefined)228+ }),229+ {} as Promise<{readonly a: number} | null>230+ )231+232+ // Return a promise or undefined.233+ exactType(234+ produce(base, draft => {235+ if (draft.a > 0) return Promise.resolve()236+ }),237+ {} as (Promise<{readonly a: number}> | {readonly a: number})238+ )239+})240+241 it("works with `void` hack", () => {205- let obj = {} as {readonly a: number}206- let res = produce(obj, s => void s.a++)207- exactType(res, obj)242+ let base = {} as {readonly a: number}243+ let copy = produce(base, s => void s.a++)244+ exactType(copy, base)245 })246 247 it("works with generic parameters", () => {248 let insert = <T>(array: ReadonlyArray<T>, index: number, elem: T) => {11 unmodified linesmodified:src/immer.d.ts+7 added-9 removed
41 unmodified lines42 }43 44 export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void45 46-type IsVoidLike<T> = T extends void | undefined ? 1 : 047-46 /** Converts `nothing` into `undefined` */49-type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T47+type FromNothing<T> = T extends Nothing ? undefined : T48 49 /** The inferred return type of `produce` */52-export type Produced<T, Return> = IsVoidLike<Return> extends 053- ? FromNothing<Return>54- : IsVoidLike<Return> extends 155- ? T56- : T | FromNothing<Exclude<Return, void>>50+export type Produced<Base, Return> = Return extends void51+ ? Base52+ : Return extends Promise<infer Result>53+ ? Promise<Result extends void ? Base : FromNothing<Result>>54+ : FromNothing<Return>55 56 type ImmutableTuple<T extends ReadonlyArray<any>> = {57 readonly [P in keyof T]: Immutable<T[P]>58 }106 unmodified lines165 *166 * Pass a function as the 2nd argument to generate Immer patches based on the167 * changes that were made.168 */171-export function finishDraft<T>(base: Draft<T>, listener?: PatchListener): T169+export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>170 171 /** Get the underlying object that is represented by the given draft */172 export function original<T>(value: T): T | void173 67 unmodified lines