Improved typings as suggested by #109. Improved typings of initialState
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 8e42349 · Browse files · 4 files·+163 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+163 added−19 removed
- tests:+90 added−0 removed
- other:+73 added−19 removed
Provenance
- Commit
aef96d2- Tree
7dd517d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
8e42349
4 files changed+163 added−19 removed
modified:__tests__/flow/flow.js.flow+29 added
29 unmodified lines30 produce({x: 3})31 32 // $ExpectError33 produce({x: 3}, [])34+35+{36+ // curried & initial arg37+ const f = produce((state, increment: number) => {38+ state.x += increment39+ }, { x: 3})40+41+ // $ExpectError Too few arguments42+ f({x : 5})43+44+ // $ExpectError45+ f({x : 5}, "test")46+47+ f({x : 5}, 3)48+49+ f(undefined, 3)50+}51+52+{53+ // curried f & no initial arg54+ const f2 = produce((state: {x:number}, increment: number) => {55+ state.x += increment56+ })57+58+ f2({x : 5}, 3)59+60+ // $ExpectError61+ f2(undefined, 3)62+}added:__tests__/flow/ts.ts+61 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"
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 f2 = produce(draft => {})f2({x: 3})
// $ExpectErrorsetAutoFreeze(3)
// $ExpectErrorconsole.log(result.y)
// $ExpectErrorproduce()
// $ExpectErrorproduce({x: 3})
// $ExpectErrorproduce({x: 3}, [])
{ // curried & initial arg const f = produce((state, increment: number) => { 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) => { state.x += increment })
f2({x : 5}, 3)
// $ExpectError f2(undefined, 3)}No newline at end of filemodified:src/immer.d.ts+45 added-10 removed
13 unmodified lines14151617181819202120212222232425262725262829303132293033343536373334383940414243444546474849505152535455565758596061626364656667686970717273749 unmodified lines13 unmodified linesexport default function<S = any>( currentState: S, recipe: (this: S, draftState: S) => void): S// curried invocations
// curried invocations with default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void, initialState?: S): (currentState: S, ...extraArgs: any[]) => S recipe: (this: S, draftState: S) => void, initialState: S): (currentState: S | undefined) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void, initialState?: S): (currentState: S, a: A) => S initialState: S): (currentState: S | undefined, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void, initialState?: S): (currentState: S, a: A, b: B) => S initialState: S): (currentState: S | undefined, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void, initialState?: S): (currentState: S, a: A, b: B, c: C) => S initialState: S): (currentState: S | undefined, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void, initialState: S): (currentState: S | undefined, ...extraArgs: any[]) => S
// curried invocations without default initial state// 0 additional argumentsexport default function<S = any>( recipe: (this: S, draftState: S) => void): (currentState: S) => S// 1 additional argument of type Aexport default function<S = any, A = any>( recipe: (this: S, draftState: S, a: A) => void): (currentState: S, a: A) => S// 2 additional arguments of types A and Bexport default function<S = any, A = any, B = any>( recipe: (this: S, draftState: S, a: A, b: B) => void): (currentState: S, a: A, b: B) => S// 3 additional arguments of types A, B and Cexport default function<S = any, A = any, B = any, C = any>( recipe: (this: S, draftState: S, a: A, b: B, c: C) => void): (currentState: S, a: A, b: B, c: C) => S// any number of additional arguments, but with loss of type safety// this may be alleviated if "variadic kinds" makes it into Typescript:// https://github.com/Microsoft/TypeScript/issues/5453export default function<S = any>( recipe: (this: S, draftState: S, ...extraArgs: any[]) => void): (currentState: S, ...extraArgs: any[]) => S/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production.9 unmodified linesmodified:src/immer.js.flow+28 added-9 removed
15 unmodified lines16 declare export default function produce<S>(17 currentState: S,18 recipe: (draftState: S) => void19 ): S20-// curried invocations20+// curried invocations with inital state21 declare export default function produce<S, A, B, C>(22 recipe: (draftState: S, a: A, b: B, c: C) => void,23- initialState?: S24-): (currentState: S, a: A, b: B, c: C) => S23+ initialState: S24+): (currentState: S | void, a: A, b: B, c: C) => S25 declare export default function produce<S, A, B>(26 recipe: (draftState: S, a: A, b: B) => void,27- initialState?: S28-): (currentState: S, a: A, b: B) => S27+ initialState: S28+): (currentState: S | void, a: A, b: B) => S29 declare export default function produce<S, A>(30 recipe: (draftState: S, a: A) => void,31- initialState?: S32-): (currentState: S) => S31+ initialState: S32+): (currentState: S | void, a: A) => S33 declare export default function produce<S>(34+ recipe: (draftState: S) => void,35+ initialState: S36+): (currentState: S | void) => S37+declare export default function produce<S>(38 recipe: (draftState: S, ...extraArgs: any[]) => void,35- initialState?: S39+ initialState: S40+): (currentState: S | void, ...extraArgs: any[]) => S41+// curried invocations without inital state42+declare export default function produce<S, A, B, C>(43+ recipe: (draftState: S, a: A, b: B, c: C) => void44+): (currentState: S, a: A, b: B, c: C) => S45+declare export default function produce<S, A, B>(46+ recipe: (draftState: S, a: A, b: B) => void47+): (currentState: S, a: A, b: B) => S48+declare export default function produce<S, A>(49+ recipe: (draftState: S, a: A) => void50+): (currentState: S, a: A) => S51+declare export default function produce<S>(52+ recipe: (draftState: S) => void53+): (currentState: S) => S54+declare export default function produce<S>(55+ recipe: (draftState: S, ...extraArgs: any[]) => void56 ): (currentState: S, ...extraArgs: any[]) => S37-57 /**58 * Automatically freezes any state trees generated by immer.59 * This protects against accidental modifications of the state tree outside of an immer function.60 * This comes with a performance impact, so it is recommended to disable this option in production.9 unmodified lines