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 fileTake it
Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
lines 1–12why, one step back1// Mapped type to remove readonly modifiers from state2// 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;12Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 13–20why, one step back13export interface Patch {14 op: "replace" | "remove",15 path: (string|number)[]16 value?: any17}1819export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void20Added flow types, implements #34Michel Weststratebefore adoption737e556
lines 21–23why, one step back21/**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 back24 * 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 back25 *Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 26–27why, one step back26 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe27 * any time it is called with the current state.Added flow types, implements #34Michel Weststratebefore adoption737e556
line 28why, one step back28 *Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 29–30why, one step back29 * In it's base form immer can receive a patch handler30 *Added flow types, implements #34Michel Weststratebefore adoption737e556
line 31why, one step back31 * @param currentState - the state to start withMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 32–33why, one step back32 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified33 * @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 34–37why, one step back34 * @returns The next state: a new state, or the current state if nothing was modified35 */36export default function<S = any>(37 currentState: S,Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 38–39why, one step back38 recipe: (this: Draft<S>, draftState: Draft<S>) => void | S,39 patchListener?: PatchListenerAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 40why, one step back40): SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 41–43why, one step back4142// curried invocations with default initial state43// 0 additional argumentsAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 44why, one step back44export default function<S = any>(Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
line 45why, one step back45 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 back46 initialState: S47): (currentState: S | undefined) => S48// 1 additional argument of type AMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 49why, one step back49export default function<S = any, A = any>(Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
line 50why, one step back50 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 back51 initialState: S52): (currentState: S | undefined, a: A) => S53// 2 additional arguments of types A and BMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 54why, one step back54export default function<S = any, A = any, B = any>(Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
line 55why, one step back55 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 back56 initialState: S57): (currentState: S | undefined, a: A, b: B) => S58// 3 additional arguments of types A, B and CMerge pull request #99 from kompot/masterMichel Weststratebefore adoption27d09f6
line 59why, one step back59export 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 back60 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 back61 initialState: S62): (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 back67 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 back68 initialState: S69): (currentState: S | undefined, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
line 70why, one step back70Improved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 71–73why, one step back71// curried invocations without default initial state72// 0 additional arguments73export default function<S = any>(Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c
line 74why, one step back74 recipe: (this: Draft<S>, draftState: Draft<S>) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 75–77why, one step back75): (currentState: S) => S76// 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 back78 recipe: (this: Draft<S>, draftState: Draft<S>, a: A) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 79–81why, one step back79): (currentState: S, a: A) => S80// 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 back82 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 83–85why, one step back83): (currentState: S, a: A, b: B) => S84// 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 back86 recipe: (this: Draft<S>, draftState: Draft<S>, a: A, b: B, c: C) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
lines 87–91why, one step back87): (currentState: S, a: A, b: B, c: C) => S88// 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 back92 recipe: (this: Draft<S>, draftState: Draft<S>, ...extraArgs: any[]) => void | SImproved typings as suggested by #109. Improved typings of initialStateMichel Weststratebefore adoptioncf32c52
line 93why, one step back93): (currentState: S, ...extraArgs: any[]) => SAdded flow types, implements #34Michel Weststratebefore adoption737e556
lines 94–106why, one step back94/**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