/** Object types that should never be mapped */ type AtomicObject = | Function | Map | WeakMap | Set | WeakSet | Promise | Date | RegExp | Boolean | Number | String type ArrayMethod = Exclude type Indices = Exclude export type DraftArray> = Array< {[P in Indices]: Draft}[Indices] > export type DraftTuple> = { [P in keyof T]: P extends Indices ? Draft : never } export type Draft = T extends never[] ? T : T extends ReadonlyArray ? T[number][] extends T ? DraftArray : DraftTuple : T extends AtomicObject ? T : T extends object ? {-readonly [P in keyof T]: Draft} : T export interface Patch { op: "replace" | "remove" | "add" path: (string | number)[] value?: any } export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void type IsVoidLike = T extends void | undefined ? 1 : 0 /** Converts `nothing` into `undefined` */ type FromNothing = Nothing extends T ? Exclude | undefined : T /** The inferred return type of `produce` */ export type Produced = IsVoidLike extends 0 ? FromNothing : IsVoidLike extends 1 ? T : T | FromNothing> type ImmutableTuple> = { readonly [P in keyof T]: Immutable } /** Convert a mutable type into a readonly type */ export type Immutable = T extends object ? T extends AtomicObject ? T : T extends ReadonlyArray ? Array extends T ? {[P in keyof T]: ReadonlyArray>}[keyof T] : ImmutableTuple : {readonly [P in keyof T]: Immutable} : T export interface IProduce { /** * The `produce` function takes a value and a "recipe function" (whose * return value often depends on the base state). The recipe function is * free to mutate its first argument however it wants. All mutations are * only ever applied to a __copy__ of the base state. * * Pass only a function to create a "curried producer" which relieves you * from passing the recipe function every time. * * Only plain objects and arrays are made mutable. All other objects are * considered uncopyable. * * Note: This function is __bound__ to its `Immer` instance. * * @param {any} base - the initial state * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @param {Function} patchListener - optional function that will be called with all the patches produced here * @returns {any} a new state, or the initial state if nothing was modified */ >( base: T, recipe: (this: D, draft: D) => Return, listener?: PatchListener ): Produced /** Curried producer with a default value */ >( recipe: (this: D, draft: D, ...rest: Rest) => Return, defaultBase: T ): (base: Immutable | undefined, ...rest: Rest) => Produced /** Curried producer with no default value */ ( recipe: (this: Draft, draft: Draft, ...rest: Rest) => Return ): (base: Immutable, ...rest: Rest) => Produced } export const produce: IProduce export default produce /** Use a class type for `nothing` so its type is unique */ declare class Nothing { // This lets us do `Exclude` private _: any } /** * The sentinel value returned by producers to replace the draft with undefined. */ export const nothing: Nothing /** * To let Immer treat your class instances as plain immutable objects * (albeit with a custom prototype), you must define either an instance property * or a static property on each of your custom classes. * * Otherwise, your class instance will never be drafted, which means it won't be * safe to mutate in a produce callback. */ export const immerable: unique symbol /** * Pass true to automatically freeze all copies created by Immer. * * By default, auto-freezing is disabled in production. */ export function setAutoFreeze(autoFreeze: boolean): void /** * Pass true to use the ES2015 `Proxy` class when creating drafts, which is * always faster than using ES5 proxies. * * By default, feature detection is used, so calling this is rarely necessary. */ export function setUseProxies(useProxies: boolean): void /** * Apply an array of Immer patches to the first argument. * * This function is a producer, which means copy-on-write is in effect. */ export function applyPatches(base: S, patches: Patch[]): S export function original(value: T): T | void export function isDraft(value: any): boolean export class Immer { constructor(config: { useProxies?: boolean autoFreeze?: boolean onAssign?: (state: ImmerState, prop: keyof any, value: any) => void onDelete?: (state: ImmerState, prop: keyof any) => void onCopy?: (state: ImmerState) => void }) /** * The `produce` function takes a value and a "recipe function" (whose * return value often depends on the base state). The recipe function is * free to mutate its first argument however it wants. All mutations are * only ever applied to a __copy__ of the base state. * * Pass only a function to create a "curried producer" which relieves you * from passing the recipe function every time. * * Only plain objects and arrays are made mutable. All other objects are * considered uncopyable. * * Note: This function is __bound__ to its `Immer` instance. * * @param {any} base - the initial state * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified * @param {Function} patchListener - optional function that will be called with all the patches produced here * @returns {any} a new state, or the initial state if nothing was modified */ produce: IProduce /** * When true, `produce` will freeze the copies it creates. */ readonly autoFreeze: boolean /** * When true, drafts are ES2015 proxies. */ readonly useProxies: boolean /** * Pass true to automatically freeze all copies created by Immer. * * By default, auto-freezing is disabled in production. */ setAutoFreeze(autoFreeze: boolean): void /** * Pass true to use the ES2015 `Proxy` class when creating drafts, which is * always faster than using ES5 proxies. * * By default, feature detection is used, so calling this is rarely necessary. */ setUseProxies(useProxies: boolean): void } export interface ImmerState { parent?: ImmerState base: T copy: T assigned: {[prop: string]: boolean} }