Skip to content
Myr

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

2 files+49 added−14 removed

modified:__tests__/produce.ts+42 added-5 removed
194 unmodified lines
195 })196 exactType(result, {} as any[])197})198199it("can produce nothing", () => {200 let result = produce({}, _ => nothing)199it("can produce an undefined value", () => {200 let base = {} as {readonly a: number}201202 // Return only nothing.203 let result = produce(base, _ => nothing)204 exactType(result, undefined)205206 // Return maybe nothing.207 let result2 = produce(base, draft => {208 if (draft.a > 0) return nothing209 })210 exactType(result2, {} as typeof base | undefined)211})212213it("can return the draft itself", () => {214 let base = {} as {readonly a: number}215 let result = produce(base, draft => draft)216217 // Currently, the `readonly` modifier is lost.218 exactType(result, {} as {a: number} | undefined)219})220221it("can return a promise", () => {222 let base = {} as {readonly a: number}223224 // 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 )231232 // 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})240241it("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})246247it("works with generic parameters", () => {248 let insert = <T>(array: ReadonlyArray<T>, index: number, elem: T) => {
11 unmodified lines
modified:src/immer.d.ts+7 added-9 removed
41 unmodified lines
42}4344export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void4546type IsVoidLike<T> = T extends void | undefined ? 1 : 04746/** Converts `nothing` into `undefined` */49type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T47type FromNothing<T> = T extends Nothing ? undefined : T4849/** The inferred return type of `produce` */52export type Produced<T, Return> = IsVoidLike<Return> extends 053 ? FromNothing<Return>54 : IsVoidLike<Return> extends 155 ? T56 : T | FromNothing<Exclude<Return, void>>50export type Produced<Base, Return> = Return extends void51 ? Base52 : Return extends Promise<infer Result>53 ? Promise<Result extends void ? Base : FromNothing<Result>>54 : FromNothing<Return>5556type ImmutableTuple<T extends ReadonlyArray<any>> = {57 readonly [P in keyof T]: Immutable<T[P]>58}
106 unmodified lines
165 *166 * Pass a function as the 2nd argument to generate Immer patches based on the167 * changes that were made.168 */171export function finishDraft<T>(base: Draft<T>, listener?: PatchListener): T169export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>170171/** Get the underlying object that is represented by the given draft */172export function original<T>(value: T): T | void173
67 unmodified lines