// @flow import produce, { enableMapSet, setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch, original } from "../../dist/index.js.flow" enableMapSet() setAutoFreeze(true) setUseProxies(true) // we really don't want this code to actually execute.. const result = produce({x: 3}, draft => { draft.x = 4 // $ExpectError console.log(draft.y) }) console.log(result.x) const result2 = produce({x: 3}, draft => { return {x: 4} }) console.log(result2.x) const f = produce( (draft): {| x: number |} => { return {x: 4} } ) f({x: 3}) const f2 = produce((draft): void => {}) f2({x: 3}) // $ExpectError setAutoFreeze(3) // $ExpectError console.log(result.y) // $ExpectError produce() // $ExpectError produce({x: 3}) // $ExpectError produce({x: 3}, []) { // curried & initial arg const f = produce( (state, increment: number): void => { state.x += increment }, {x: 3} ) // $ExpectError Too few arguments f({x: 5}) // $ExpectError f({x: 5}, "test") f({x: 5}, 3) f(undefined, 3) } { // curried f & no initial arg const f2 = produce( (state: {| x: number |}, increment: number): void => { state.x += increment } ) f2({x: 5}, 3) // $ExpectError f2(undefined, 3) } type State = {| user: {| age: number |} |} { declare function setState(updater: (State) => $Shape): void // curried use with setState, infer type of draft setState(produce(draft => { if (draft.user.age === 10) { return } draft.user.age += 1 // $ExpectError draft.user.name = "missing" })) } { // $ExpectError produce2({x: 3}) const result = produce2({x: 3}, draft => { draft.x = 4 // $ExpectError console.log(draft.y) }) } { let p: Patch[] = [] produce( {}, d => {}, (patches, inverse) => { p = patches } ) applyPatches({}, p) } { produce({x: 3}, draftState => { let a = original(draftState) if (a) { a.x // $ExpectError a.y } }) } { const arr: Array = [1] produce(arr, draftState => { const a = original(draftState) if (a) { const b: number = a[0] // $ExpectError const c: string = a[0] } }) } function testMap(state: Map) { // normal state = produce(state, draft => { if (draft.has(1)) { return } // $ExpectError draft.set(1, 2) draft.set(1, "tada") }) // curried, no initial state const f1 = produce(draft=> { draft.set(2, "ok") }) state = f1(state) // curried, with initial state const f2 = produce(draft=> { draft.set(2, "ok") }, state) state = f2(state) state = f2(undefined) } function testSet(state: Set) { // normal state = produce(state, draft => { if (draft.has(1)) { return } // $ExpectError draft.add("me") draft.add(100) }) // curried, no initial state const f1 = produce(draft => { draft.add(draft.size) }) state = f1(state) // curried, with initial state const f2 = produce(draft => { draft.add(draft.size) }, state) state = f2(state) state = f2(undefined) }