Skip to content
Myr

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 adoptioncf32c52

History of this file

Take it

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 1–3why, one step back
1/**
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 adoption2cb5496

line 4why, one step back
4 * 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 back
5 *

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 6–7why, one step back
6 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe
7 * any time it is called with the current state.

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 8–9why, one step back
8 *
9 * @param currentState - the state to start with

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 10–11why, one step back
10 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
11 * @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 undefined

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 12–15why, one step back
12 * @returns The next state: a new state, or the current state if nothing was modified
13 */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 back
16 recipe: (this: S, draftState: S) => void

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 17why, one step back
17): S

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 18–20why, one step back
18
19// curried invocations with default initial state20// 0 additional arguments

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 21why, one step back
21export default function<S = any>(

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 22–25why, one step back
22 recipe: (this: S, draftState: S) => void,
23 initialState: S24): (currentState: S | undefined) => S25// 1 additional argument of type A

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 26why, one step back
26export default function<S = any, A = any>(

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 27why, one step back
27 recipe: (this: S, draftState: S, a: A) => void,

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 28–30why, one step back
28 initialState: S
29): (currentState: S | undefined, a: A) => S30// 2 additional arguments of types A and B

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 31why, one step back
31export default function<S = any, A = any, B = any>(

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 32why, one step back
32 recipe: (this: S, draftState: S, a: A, b: B) => void,

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 33–35why, one step back
33 initialState: S
34): (currentState: S | undefined, a: A, b: B) => S35// 3 additional arguments of types A, B and C

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

line 36why, one step back
36export default function<S = any, A = any, B = any, C = any>(

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 37why, one step back
37 recipe: (this: S, draftState: S, a: A, b: B, c: C) => void,

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 38–46why, one step back
38 initialState: S
39): (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[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 47why, one step back
47

Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52

lines 48–70why, one step back
48// curried invocations without default initial state
49// 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[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 71–83why, one step back
71/**
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