import {Nothing, DRAFT_STATE} from "./common" import {SetState} from "./set" import {MapState} from "./map" import {ProxyObjectState, ProxyArrayState} from "./proxy" import {ES5ObjectState, ES5ArrayState} from "./es5" import {ImmerScope} from "./scope" export type Objectish = AnyObject | AnyArray | AnyMap | AnySet export type ObjectishNoSet = AnyObject | AnyArray | AnyMap export type AnyObject = {[key: string]: any} export type AnyArray = Array export type AnySet = Set export type AnyMap = Map export type DraftType = | "proxy_object" | "proxy_array" | "es5_object" | "es5_array" | "map" | "set" export interface ImmerBaseState { parent?: ImmerState scope: ImmerScope modified: boolean finalized: boolean isManual: boolean } export type ImmerState = | ProxyObjectState | ProxyArrayState | ES5ObjectState | ES5ArrayState | MapState | SetState // The _internal_ type used for drafts (not to be confused with Draft, which is public facing) export type Drafted = { [DRAFT_STATE]: T } & Base type Tail = ((...t: T) => any) extends ( _: any, ...tail: infer TT ) => any ? TT : [] /** Object types that should never be mapped */ type AtomicObject = | Function | WeakMap | WeakSet | Promise | Date | RegExp | Boolean | Number | String export type Draft = T extends AtomicObject ? T : T extends Map ? DraftMap : T extends Set ? DraftSet : T extends object ? {-readonly [K in keyof T]: Draft} : T // Inline these in ts 3.7 interface DraftMap extends Map, Draft> {} // Inline these in ts 3.7 interface DraftSet extends Set> {} /** Convert a mutable type into a readonly type */ export type Immutable = T extends AtomicObject ? T : T extends Map // Ideally, but wait for TS 3.7: ? Omit, "set" | "delete" | "clear"> ? ImmutableMap : T extends Set // Ideally, but wait for TS 3.7: ? Omit, "add" | "delete" | "clear"> ? ImmutableSet : T extends object ? {readonly [K in keyof T]: Immutable} : T interface ImmutableMap extends Map, Immutable> {} interface ImmutableSet extends Set> {} export interface Patch { op: "replace" | "remove" | "add" path: (string | number)[] value?: any } export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void /** Converts `nothing` into `undefined` */ type FromNothing = T extends Nothing ? undefined : T /** The inferred return type of `produce` */ export type Produced = Return extends void ? Base : Return extends Promise ? Promise> : FromNothing /** * 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 */ export interface IProduce { /** Curried producer */ < Recipe extends (...args: any[]) => any, Params extends any[] = Parameters, T = Params[0] >( recipe: Recipe ): >( base: Base, ...rest: Tail ) => Produced> // ^ by making the returned type generic, the actual type of the passed in object is preferred // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct! /** Curried producer with initial state */ < Recipe extends (...args: any[]) => any, Params extends any[] = Parameters, T = Params[0] >( recipe: Recipe, initialState: Immutable ): >( base?: Base, ...rest: Tail ) => Produced> /** Normal producer */ , Return = void>( base: Base, recipe: (draft: D) => Return, listener?: PatchListener ): Produced } /** * Like `produce`, but instead of just returning the new state, * a tuple is returned with [nextState, patches, inversePatches] * * Like produce, this function supports currying */ export interface IProduceWithPatches { /** Curried producer */ < Recipe extends (...args: any[]) => any, Params extends any[] = Parameters, T = Params[0] >( recipe: Recipe ): >( base: Base, ...rest: Tail ) => [Produced>, Patch[], Patch[]] // ^ by making the returned type generic, the actual type of the passed in object is preferred // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct! /** Curried producer with initial state */ < Recipe extends (...args: any[]) => any, Params extends any[] = Parameters, T = Params[0] >( recipe: Recipe, initialState: Immutable ): >( base?: Base, ...rest: Tail ) => [Produced>, Patch[], Patch[]] /** Normal producer */ , Return = void>( base: Base, recipe: (draft: D) => Return ): [Produced, Patch[], Patch[]] } // Backward compatibility with --target es5 // TODO: still needed? // declare global { // interface Set {} // interface Map {} // interface WeakSet {} // interface WeakMap {} // }