/** Object types that should never be mapped */ type AtomicObject = | Function | Map | WeakMap | Set | WeakSet | Promise | Date | RegExp | Boolean | Number | String /** Use type inference to know when an array is finite */ type IsFinite = T extends never[] ? true : T extends ReadonlyArray ? (U[] extends T ? false : true) : true export type DraftObject = T extends object ? T extends AtomicObject ? T : {-readonly [P in keyof T]: Draft} : T export type DraftArray = Array< T extends ReadonlyArray ? {[P in keyof T]: Draft}[keyof T] : DraftObject > export type DraftTuple = { [P in keyof T]: T[P] extends T[number] ? Draft : never } export type Draft = T extends any[] ? IsFinite extends true ? DraftTuple : DraftArray : T extends ReadonlyArray ? DraftArray : T extends object ? DraftObject : T export interface Patch { op: "replace" | "remove" | "add" path: (string | number)[] value?: any } export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void export interface IProduce { /** * Immer takes a state, and runs a function against it. * That function can freely mutate the state, as it will create copies-on-write. * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned. * * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe * any time it is called with the current state. * * @param currentState - the state to start with * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified * @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 * @returns The next state: a new state, or the current state if nothing was modified */ ( currentState: S, recipe: (this: Draft, draftState: Draft) => void | R, listener?: PatchListener ): R /** Curried producer with an initial state */ ( recipe: (this: Draft, draftState: Draft) => void | R, initialState: S ): (currentState: S | undefined) => R /** Curried producer with no initial state */ ( recipe: ( this: Draft, draftState: Draft, ...extraArgs: Args ) => void | R ): (currentState: S, ...extraArgs: Args) => R } export const produce: IProduce export default produce export const nothing: undefined /** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production. * It is by default enabled. */ export function setAutoFreeze(autoFreeze: boolean): void /** * Manually override whether proxies should be used. * By default done by using feature detection */ export function setUseProxies(useProxies: boolean): void export function applyPatches(state: S, patches: Patch[]): S export function original(value: T): T | void export function isDraft(value: any): boolean