Skip to content
Myr

fix(ts): return type of produce()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

To avoid certain edge cases, we must use the expected draft type (instead of the base type) when determining the return type.

authored by aleclarson · committed by aleclarson · parent a52614a · Browse files · 1 file+14 added−24 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−24 removed

  • other:+14 added−24 removed

1 file+14 added−24 removed

modified:src/immer.d.ts+14 added-24 removed
54 unmodified lines
55/** Converts `nothing` into `undefined` */56type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T5758/** The inferred return type of `produce` */59type Produced<Base, Return> = 1 extends HasVoidLike<Return>59type Produced<T, Return> = 1 extends HasVoidLike<Return>60 ? 1 extends IsVoidLike<Return>61 ? Immutable<Base>62 : Immutable<Base> | FromNothing<Exclude<Return, void>>61 ? Immutable<T>62 : Immutable<T> | FromNothing<Exclude<Return, void>>63 : FromNothing<Return>6465type ImmutableTuple<T extends ReadonlyArray<any>> = {66 readonly [P in keyof T]: Immutable<T[P]>
29 unmodified lines
96 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified97 * @param {Function} patchListener - optional function that will be called with all the patches produced here98 * @returns {any} a new state, or the initial state if nothing was modified99 */100 <Base, Proxy = Draft<Base>, Return = void>(101 base: Base,102 recipe: (this: Proxy, draft: Proxy) => Return,100 <T = any, D = Draft<T>, Return = void>(101 base: T,102 recipe: (this: D, draft: D) => Return,103 listener?: PatchListener104 ): Produced<Base, Return>104 ): Produced<D, Return>105106 /** Curried producer with a default value */107 <Default = any, Base = Default, Rest extends any[] = [], Return = void>(108 recipe: (109 this: Draft<Base>,110 draft: Draft<Base>,111 ...rest: Rest112 ) => Return,113 defaultBase: Default114 ): <T>(115 base: (Draft<T> extends Draft<Base> ? T : Base) | undefined,116 ...rest: Rest117 ) => Produced<Base, Return>107 <T = any, D = Draft<T>, Rest extends any[] = [], Return = void>(108 recipe: (this: D, draft: D, ...rest: Rest) => Return,109 defaultBase: T110 ): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>111112 /** Curried producer with no default value */120 <Base = any, Rest extends any[] = [], Return = void>(121 recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return122 ): <T>(123 base: Draft<T> extends Draft<Base> ? T : Base,124 ...rest: Rest125 ) => Produced<Base, Return>113 <T = any, Rest extends any[] = [], Return = void>(114 recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return115 ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>116}117118export const produce: IProduce119export default produce
95 unmodified lines