Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

106 lines · 4.8 KiB · TypeScript

Last changed by

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

History of this file

Take it

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

lines 1–12why, one step back
1// Mapped type to remove readonly modifiers from state
2// Based on https://github.com/Microsoft/TypeScript/blob/d4dc67aab233f5a8834dff16531baf99b16fea78/tests/cases/conformance/types/conditional/conditionalTypes1.ts#L120-L1293export type DraftObject<T> = {4 -readonly [P in keyof T]: Draft<T[P]>;5};6export interface DraftArray<T> extends Array<Draft<T>> { }7export type Draft<T> =8 T extends any[] ? DraftArray<T[number]> :9 T extends ReadonlyArray<any> ? DraftArray<T[number]> :10 T extends object ? DraftObject<T> :11 T;12

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 13–20why, one step back
13export interface Patch {
14 op: "replace" | "remove",15 path: (string|number)[]16 value?: any17}1819export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void20

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 21–23why, one step back
21/**
22 * Immer takes a state, and runs a function against it.23 * 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 24why, one step back
24 * 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 25why, one step back
25 *

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

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

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 28why, one step back
28 *

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 29–30why, one step back
29 * In it's base form immer can receive a patch handler
30 *

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 31why, one step back
31 * @param currentState - the state to start with

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

lines 32–33why, one step back
32 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified
33 * @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 34–37why, one step back
34 * @returns The next state: a new state, or the current state if nothing was modified
35 */36export default function<S = any>(37 currentState: S,

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 38–39why, one step back
38 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,
39 patchListener?: PatchListener

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 40why, one step back
40): S

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

lines 41–43why, one step back
41
42// curried invocations with default initial state43// 0 additional arguments

Added flow types, implements #34Michel Weststratebefore adoption737e556

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

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 45why, one step back
45 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,

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

lines 46–48why, one step back
46 initialState: S
47): (currentState: S | undefined) => S48// 1 additional argument of type A

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

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

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 50why, one step back
50 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S,

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

lines 51–53why, one step back
51 initialState: S
52): (currentState: S | undefined, a: A) => S53// 2 additional arguments of types A and B

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

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

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

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

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

lines 56–58why, one step back
56 initialState: S
57): (currentState: S | undefined, a: A, b: B) => S58// 3 additional arguments of types A, B and C

Merge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6

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

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

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

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

lines 61–66why, one step back
61 initialState: S
62): (currentState: S | undefined, a: A, b: B, c: C) => S63// any number of additional arguments, but with loss of type safety64// this may be alleviated if "variadic kinds" makes it into Typescript:65// https://github.com/Microsoft/TypeScript/issues/545366export default function<S = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 67why, one step back
67 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S,

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

lines 68–69why, one step back
68 initialState: S
69): (currentState: S | undefined, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

line 70why, one step back
70

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

lines 71–73why, one step back
71// curried invocations without default initial state
72// 0 additional arguments73export default function<S = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 74why, one step back
74 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S

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

lines 75–77why, one step back
75): (currentState: S) => S
76// 1 additional argument of type A77export default function<S = any, A = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 78why, one step back
78 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | S

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

lines 79–81why, one step back
79): (currentState: S, a: A) => S
80// 2 additional arguments of types A and B81export default function<S = any, A = any, B = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

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

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

lines 83–85why, one step back
83): (currentState: S, a: A, b: B) => S
84// 3 additional arguments of types A, B and C85export default function<S = any, A = any, B = any, C = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

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

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

lines 87–91why, one step back
87): (currentState: S, a: A, b: B, c: C) => S
88// any number of additional arguments, but with loss of type safety89// this may be alleviated if "variadic kinds" makes it into Typescript:90// https://github.com/Microsoft/TypeScript/issues/545391export default function<S = any>(

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 92why, one step back
92 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | S

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

line 93why, one step back
93): (currentState: S, ...extraArgs: any[]) => S

Added flow types, implements #34Michel Weststratebefore adoption737e556

lines 94–106why, one step back
94/**
95 * Automatically freezes any state trees generated by immer.96 * This protects against accidental modifications of the state tree outside of an immer function.97 * This comes with a performance impact, so it is recommended to disable this option in production.98 * It is by default enabled.99 */100export function setAutoFreeze(autoFreeze: boolean): void101102/**103 * Manually override whether proxies should be used.104 * By default done by using feature detection105 */106export function setUseProxies(useProxies: boolean): void