immer.d.ts
This file
Path
src/immer.d.ts
188 lines · 6.0 KiB · TypeScript
Last changed by
fix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
History of this fileTake it
fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 1–75why, one step back1/** Object types that should never be mapped */2type AtomicObject =3 | Function4 | Map<any, any>5 | WeakMap<any, any>6 | Set<any>7 | WeakSet<any>8 | Promise<any>9 | Date10 | RegExp11 | Boolean12 | Number13 | String1415/** Use type inference to know when an array is finite */16type IsFinite<T extends any[]> = T extends never[]17 ? true18 : T extends ReadonlyArray<infer U>19 ? (U[] extends T ? false : true)20 : true2122export type DraftObject<T> = T extends object23 ? T extends AtomicObject24 ? T25 : {-readonly [P in keyof T]: Draft<T[P]>}26 : T2728export type DraftArray<T> = Array<29 T extends ReadonlyArray<any>30 ? {[P in keyof T]: Draft<T>}[keyof T]31 : DraftObject<T>32>3334export type DraftTuple<T extends any[]> = {35 [P in keyof T]: T[P] extends T[number] ? Draft<T[P]> : never36}3738export type Draft<T> = T extends any[]39 ? IsFinite<T> extends true40 ? DraftTuple<T>41 : DraftArray<T[number]>42 : T extends ReadonlyArray<any>43 ? DraftArray<T[number]>44 : T extends object45 ? DraftObject<T>46 : T4748export interface Patch {49 op: "replace" | "remove" | "add"50 path: (string | number)[]51 value?: any52}5354export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void5556export interface IProduce {57 /**58 * The `produce` function takes a value and a "recipe function" (whose59 * return value often depends on the base state). The recipe function is60 * free to mutate its first argument however it wants. All mutations are61 * only ever applied to a __copy__ of the base state.62 *63 * Pass only a function to create a "curried producer" which relieves you64 * from passing the recipe function every time.65 *66 * Only plain objects and arrays are made mutable. All other objects are67 * considered uncopyable.68 *69 * Note: This function is __bound__ to its `Immer` instance.70 *71 * @param {any} base - the initial state72 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified73 * @param {Function} patchListener - optional function that will be called with all the patches produced here74 * @returns {any} a new state, or the initial state if nothing was modified75 */fix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
lines 76–78why, one step back76 <State, Result = any>(77 currentState: State,78 recipe: (this: Draft<State>, draft: Draft<State>) => Result,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 79why, one step back79 listener?: PatchListenerfix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
line 80why, one step back80 ): void extends Result ? State : Resultfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 81–82why, one step back8182 /** Curried producer with an initial state */fix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
lines 83–86why, one step back83 <State, Result = any>(84 recipe: (this: Draft<State>, draft: Draft<State>) => void | Result,85 defaultBase: State86 ): (base: State | undefined) => void extends Result ? State : Resultfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 87–88why, one step back8788 /** Curried producer with no initial state */fix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
line 89why, one step back89 <State, Result = any, Args extends any[] = any[]>(fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 90why, one step back90 recipe: (fix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
lines 91–92why, one step back91 this: Draft<State>,92 draft: Draft<State>,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 93why, one step back93 ...extraArgs: Argsfix(ts): produce functionaleclarsonbefore adoptionfa5bf5d
lines 94–95why, one step back94 ) => void | Result95 ): (base: State, ...extraArgs: Args) => void extends Result ? State : Resultfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 96–188why, one step back96}9798export const produce: IProduce99export default produce100101/**102 * The sentinel value returned by producers to replace the draft with undefined.103 */104export const nothing: undefined105106/**107 * Pass true to automatically freeze all copies created by Immer.108 *109 * By default, auto-freezing is disabled in production.110 */111export function setAutoFreeze(autoFreeze: boolean): void112113/**114 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is115 * always faster than using ES5 proxies.116 *117 * By default, feature detection is used, so calling this is rarely necessary.118 */119export function setUseProxies(useProxies: boolean): void120121/**122 * Apply an array of Immer patches to the first argument.123 *124 * This function is a producer, which means copy-on-write is in effect.125 */126export function applyPatches<S>(base: S, patches: Patch[]): S127128export function original<T>(value: T): T | void129130export function isDraft(value: any): boolean131132export class Immer {133 constructor(config: {134 useProxies?: boolean135 autoFreeze?: boolean136 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void137 onDelete?: (state: ImmerState, prop: keyof any) => void138 onCopy?: (state: ImmerState) => void139 })140 /**141 * The `produce` function takes a value and a "recipe function" (whose142 * return value often depends on the base state). The recipe function is143 * free to mutate its first argument however it wants. All mutations are144 * only ever applied to a __copy__ of the base state.145 *146 * Pass only a function to create a "curried producer" which relieves you147 * from passing the recipe function every time.148 *149 * Only plain objects and arrays are made mutable. All other objects are150 * considered uncopyable.151 *152 * Note: This function is __bound__ to its `Immer` instance.153 *154 * @param {any} base - the initial state155 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified156 * @param {Function} patchListener - optional function that will be called with all the patches produced here157 * @returns {any} a new state, or the initial state if nothing was modified158 */159 produce: IProduce160 /**161 * When true, `produce` will freeze the copies it creates.162 */163 readonly autoFreeze: boolean164 /**165 * When true, drafts are ES2015 proxies.166 */167 readonly useProxies: boolean168 /**169 * Pass true to automatically freeze all copies created by Immer.170 *171 * By default, auto-freezing is disabled in production.172 */173 setAutoFreeze(autoFreeze: boolean): void174 /**175 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is176 * always faster than using ES5 proxies.177 *178 * By default, feature detection is used, so calling this is rarely necessary.179 */180 setUseProxies(useProxies: boolean): void181}182183export interface ImmerState<T = any> {184 parent?: ImmerState185 base: T186 copy: T187 assigned: {[prop: string]: boolean}188}