Skip to content
Myr

[Flow] Fix two issues with curried produce

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

* Draft parameter can be undefined (unless type annotation is provided). * Not possible to return undefined.

authored by Björgvin Ragnarsson · committed by Björgvin Ragnarsson · parent 334175f · Browse files · 2 files+25 added−37 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+25 added−37 removed

  • tests:+17 added−2 removed
  • other:+8 added−35 removed

2 files+25 added−37 removed

modified:__tests__/flow/flow.js.flow+17 added-2 removed
4 unmodified lines
5 produce as produce2,6 applyPatches,7 Patch,8 original9} from "../../dist/immer.js"9} from "../../dist/index.js.flow"1011setAutoFreeze(true)12setUseProxies(true)13
71 unmodified lines
85 // $ExpectError86 f2(undefined, 3)87}8889type State = {| user: {| age: number |} |}90{91 declare function setState(updater: (State) => $Shape<State>): void92 // curried use with setState, infer type of draft93 setState(produce(draft => {94 if (draft.user.age === 10) {95 return96 }97 draft.user.age += 198 // $ExpectError99 draft.user.name = "missing"100 }))101}102103{104 // $ExpectError105 produce2({x: 3})106107 const result = produce2({x: 3}, draft => {
27 unmodified lines
135 })136}137138{139 const arr: Array<number> = [1];125 produce([1], draftState => {140 produce(arr, draftState => {141 const a = original(draftState)142 if (a) {143 const b: number = a[0]144 // $ExpectError
4 unmodified lines
modified:src/types/index.js.flow+8 added-35 removed
6 unmodified lines
7}89export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void1011type Base = {...} | Array<any> | Map<any> | Set<any>;12interface IProduce {13 /**14 * Immer takes a state, and runs a function against it.15 * That function can freely mutate the state, as it will create copies-on-write.
6 unmodified lines
22 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified23 * @param initialState - if a curried function is created and this argument was given, it will be used as fallback if the curried function is called with a state of undefined24 * @returns The next state: a new state, or the current state if nothing was modified25 */25 <S>(26 <S: Base>(27 currentState: S,28 recipe: (draftState: S) => S | void,29 patchListener?: PatchListener30 ): S;31 // curried invocations with inital state31 <S, A, B, C>(32 recipe: (draftState: S, a: A, b: B, c: C) => S | void,32 <S: Base, A = void, B = void, C = void>(33 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void,34 initialState: S34 ): (currentState: S | void, a: A, b: B, c: C) => S;35 <S, A, B>(36 recipe: (draftState: S, a: A, b: B) => S | void,37 initialState: S38 ): (currentState: S | void, a: A, b: B) => S;39 <S, A>(40 recipe: (draftState: S, a: A) => S | void,41 initialState: S42 ): (currentState: S | void, a: A) => S;43 <S>(44 recipe: (draftState: S) => S | void,45 initialState: S46 ): (currentState: S | void) => S;47 <S>(48 recipe: (draftState: S, ...extraArgs: any[]) => S | void,49 initialState: S50 ): (currentState: S | void, ...extraArgs: any[]) => S;35 ): (currentState: S | void, a: A, b: B, c: C, ...extraArgs: any[]) => S;36 // curried invocations without inital state52 <S, A, B, C>(53 recipe: (draftState: S, a: A, b: B, c: C) => S | void54 ): (currentState: S, a: A, b: B, c: C) => S;55 <S, A, B>(56 recipe: (draftState: S, a: A, b: B) => S | void57 ): (currentState: S, a: A, b: B) => S;58 <S, A>(59 recipe: (draftState: S, a: A) => S | void60 ): (currentState: S, a: A) => S;61 <S>(62 recipe: (draftState: S) => S | void63 ): (currentState: S) => S;64 <S>(65 recipe: (draftState: S, ...extraArgs: any[]) => S | void66 ): (currentState: S, ...extraArgs: any[]) => S;37 <S: Base, A = void, B = void, C = void>(38 recipe: (draftState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S | void39 ): (currentState: S, a: A, b: B, c: C, ...extraArgs: any[]) => S;40}4142declare export var produce: IProduce43declare export default IProduce
37 unmodified lines