immer.d.ts
This file
Path
src/immer.d.ts
83 lines · 3.8 KiB · TypeScript
Last changed by
Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
History of this fileTake it
Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 1–3why, one step back1/**2 * Immer takes a state, and runs a function against it.3 * That function can freely mutate the state, as it will create copies-on-write.Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
line 4why, one step back4 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.Added flow types, implements #34Michel Weststratebefore adoption737e556
line 5why, one step back5 *Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
lines 6–7why, one step back6 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe7 * any time it is called with the current state.Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 8–9why, one step back8 *9 * @param currentState - the state to start withMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
lines 10–11why, one step back10 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified11 * @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 undefinedAdded flow types, implements #34Michel Weststratebefore adoption737e556
lines 12–15why, one step back12 * @returns The next state: a new state, or the current state if nothing was modified13 */14export default function<S = any>(15 currentState: S,Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 16why, one step back16 recipe: (this: S, draftState: S) => voidAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 17why, one step back17): SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 18–20why, one step back1819// curried invocations with default initial state20// 0 additional argumentsAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 21why, one step back21export default function<S = any>(Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 22–25why, one step back22 recipe: (this: S, draftState: S) => void,23 initialState: S24): (currentState: S | undefined) => S25// 1 additional argument of type AMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 26why, one step back26export default function<S = any, A = any>(Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
line 27why, one step back27 recipe: (this: S, draftState: S, a: A) => void,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 28–30why, one step back28 initialState: S29): (currentState: S | undefined, a: A) => S30// 2 additional arguments of types A and BMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 31why, one step back31export default function<S = any, A = any, B = any>(Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
line 32why, one step back32 recipe: (this: S, draftState: S, a: A, b: B) => void,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 33–35why, one step back33 initialState: S34): (currentState: S | undefined, a: A, b: B) => S35// 3 additional arguments of types A, B and CMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 36why, one step back36export default function<S = any, A = any, B = any, C = any>(Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption8e42349
line 37why, one step back37 recipe: (this: S, draftState: S, a: A, b: B, c: C) => void,Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 38–46why, one step back38 initialState: S39): (currentState: S | undefined, a: A, b: B, c: C) => S40// any number of additional arguments, but with loss of type safety41// this may be alleviated if "variadic kinds" makes it into Typescript:42// https://github.com/Microsoft/TypeScript/issues/545343export default function<S = any>(44 recipe: (this: S, draftState: S, ...extraArgs: any[]) => void,45 initialState: S46): (currentState: S | undefined, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 47why, one step back47Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptionaef96d2
lines 48–70why, one step back48// curried invocations without default initial state49// 0 additional arguments50export default function<S = any>(51 recipe: (this: S, draftState: S) => void52): (currentState: S) => S53// 1 additional argument of type A54export default function<S = any, A = any>(55 recipe: (this: S, draftState: S, a: A) => void56): (currentState: S, a: A) => S57// 2 additional arguments of types A and B58export default function<S = any, A = any, B = any>(59 recipe: (this: S, draftState: S, a: A, b: B) => void60): (currentState: S, a: A, b: B) => S61// 3 additional arguments of types A, B and C62export default function<S = any, A = any, B = any, C = any>(63 recipe: (this: S, draftState: S, a: A, b: B, c: C) => void64): (currentState: S, a: A, b: B, c: C) => S65// any number of additional arguments, but with loss of type safety66// this may be alleviated if "variadic kinds" makes it into Typescript:67// https://github.com/Microsoft/TypeScript/issues/545368export default function<S = any>(69 recipe: (this: S, draftState: S, ...extraArgs: any[]) => void70): (currentState: S, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
lines 71–83why, one step back71/**72 * Automatically freezes any state trees generated by immer.73 * This protects against accidental modifications of the state tree outside of an immer function.74 * This comes with a performance impact, so it is recommended to disable this option in production.75 * It is by default enabled.76 */77export function setAutoFreeze(autoFreeze: boolean): void7879/**80 * Manually override whether proxies should be used.81 * By default done by using feature detection82 */83export function setUseProxies(useProxies: boolean): void