Skip to content
Myr

fix: improve flow types

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

authored by Michel Weststrate · committed by GitHub · parents 5a968e8, 0dbcfe7 · Browse files · 5 files+35 added−44 removed
This commit

State

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

Diff stats

5 files+35 added−44 removed

  • tests:+22 added−4 removed
  • other:+13 added−40 removed

5 files+35 added−44 removed

modified:__tests__/flow/.flowconfig+3 added
1[include]2../../dist34[lints]5ambiguous-object-type=error6deprecated-type=error7untyped-import=error89[options]10suppress_comment= \\(.\\|\n\\)*\\$ExpectError11include_warnings=true
modified:__tests__/flow/flow.js.flow+19 added-4 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
13 unmodified lines
2728console.log(result2.x)2930const f = produce(31 (draft): {x: number} => {31 (draft): {| x: number |} => {32 return {x: 4}33 }34)35f({x: 3})
38 unmodified lines
7475{76 // curried f & no initial arg77 const f2 = produce(78 (state: {x: number}, increment: number): void => {78 (state: {| x: number |}, increment: number): void => {79 state.x += increment80 }81 )82
2 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
modified:package.json+1 added-1 removed
modified:yarn.lock+4 added-4 removed
4986 unmodified lines
4987 version "1.0.3"4988 resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.3.tgz#c1283ac9f27b368abc1e36d1ff7b04501a30356b"4989 integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==49904991flow-bin@^0.68.0:4992 version "0.68.0"4993 resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.68.0.tgz#86c2d14857d306eb2e85e274f2eebf543564f623"4994 integrity sha1-hsLRSFfTBusuheJ08u6/VDVk9iM=4991flow-bin@^0.123.0:4992 version "0.123.0"4993 resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.123.0.tgz#7ba61a0b8775928cf4943ccf78eed2b1b05f7b3a"4994 integrity sha512-Ylcf8YDIM/KrqtxkPuq+f8O+6sdYA2Nuz5f+sWHlp539DatZz3YMcsO1EiXaf1C11HJgpT/3YGYe7xZ9/UZmvQ==49954996flush-write-stream@^1.0.0:4997 version "1.1.1"4998 resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8"
7447 unmodified lines