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
Provenance
- Commit
427040c- Tree
5d14df5- Author
Michel Weststrate <mweststrate@fb.com>- Committer
GitHub <noreply@github.com>- Parent 1
5a968e8- Parent 2
0dbcfe7
5 files changed+35 added−44 removed
modified:__tests__/flow/.flowconfig+3 added
1 [include]2 ../../dist3 4 [lints]5+ambiguous-object-type=error6+deprecated-type=error7 untyped-import=error8 9 [options]10 suppress_comment= \\(.\\|\n\\)*\\$ExpectError11+include_warnings=truemodified:__tests__/flow/flow.js.flow+19 added-4 removed
4 unmodified lines5 produce as produce2,6 applyPatches,7 Patch,8 original9-} from "../../dist/immer.js"9+} from "../../dist/index.js.flow"10 11 setAutoFreeze(true)12 setUseProxies(true)13 13 unmodified lines27 28 console.log(result2.x)29 30 const f = produce(31- (draft): {x: number} => {31+ (draft): {| x: number |} => {32 return {x: 4}33 }34 )35 f({x: 3})38 unmodified lines74 75 {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 lines85 // $ExpectError86 f2(undefined, 3)87 }88 89+type 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+}102+103+{104 // $ExpectError105 produce2({x: 3})106 107 const result = produce2({x: 3}, draft => {27 unmodified lines135 })136 }137 138 {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 // $ExpectError4 unmodified linesmodified:src/types/index.js.flow+8 added-35 removed
6 unmodified lines7 }8 9 export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void10 11+type Base = {...} | Array<any> | Map<any> | Set<any>;12 interface 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 lines22 * @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 }41 42 declare export var produce: IProduce43 declare export default IProduce37 unmodified linesmodified:package.json+1 added-1 removed
74 unmodified lines7576777879798081828316 unmodified lines74 unmodified lines "coveralls": "^3.0.0", "cpx2": "^2.0.0", "cross-env": "^5.1.3", "deep-freeze": "^0.0.1", "flow-bin": "^0.68.0", "flow-bin": "^0.123.0", "husky": "^1.2.0", "immutable": "^3.8.2", "import-size": "^1.0.2", "jest": "^25.1.0",16 unmodified linesmodified:yarn.lock+4 added-4 removed
4986 unmodified lines4987 version "1.0.3"4988 resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.3.tgz#c1283ac9f27b368abc1e36d1ff7b04501a30356b"4989 integrity sha512-dVsPA/UwQ8+2uoFe5GHtiBMu48dWLTdsuEd7CKGlZlD78r1TTWBvDuFaFGKCo/ZfEr95Uk56vZoX86OsHkUeIg==4990 4991-flow-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=4991+flow-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==4995 4996 flush-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