/** * Creates an object copy by mutating a draft Object/Array/Map/Set or any of its descendants. */ export declare function update(obj: T, updater: (draft: Draft) => NoReturn): T; export declare function clone(obj: T): any; interface DraftArray { readonly length: number; [n: number]: Draft | undefined; /** Iterator */ [Symbol.iterator](): IterableIterator; /** * Returns an iterable of [key, value] pairs for every entry in the array */ entries(): IterableIterator<[number, T]>; /** * Returns an iterable of keys in the array */ keys(): IterableIterator; /** * Returns an iterable of values in the array */ values(): IterableIterator; /** * Performs the specified action for each element in an array. * Note: This never create an object draft. Access the item by index again to mutate it. */ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; indexOf(searchElement: T, fromIndex?: number): number; includes(searchElement: T, fromIndex?: number): boolean; some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number; every(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): DraftArray; filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): DraftArray; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; /** * Adds an item at the front of the Array. */ prepend(item: T): void; /** * Adds an item at the back of the Array. */ append(item: T): void; /** * Inserts an item at the specified index in the Array. */ insert(item: T, index: number): void; /** * Runs a predicate for each item of the Array. * If it returns true, a Draft item is created and given to updateFunction, ready to be mutated. */ updateIf(predicate: (item: T, index: number) => boolean, updateFunction: (item: Draft, index: number) => NoReturn): void; /** * Removes all items satisfying a predicate. This is the mutating (at the draft level) equivalent of filter(). */ removeIf(predicate: (item: T, index: number) => boolean): void; } interface DraftMap extends Omit, 'get'> { /** Returns an iterable of entries in the map. */ [Symbol.iterator](): IterableIterator<[K, V]>; /** * If the key is found, returns the drafted value, else return undefined. */ get(key: K): Draft | undefined; /** * If the key is found, run the drafted value through an update function. * For primitives, the update function must return a new value whereas for objects, the drafted value can be modified directly. */ updateValue(key: K, updater: (value: Draft) => V extends AtomicObject ? V : NoReturn): void; } interface DraftSet extends Set { } /** Types that should never be drafted */ export declare type AtomicObject = Function | Promise | Date | RegExp | Boolean | Number | String; declare type IsTuple> = T extends never[] ? true : T extends ReadonlyArray ? U[] extends T ? false : true : true; declare type WritableDraft = { -readonly [K in keyof T]: Draft; }; export declare type Draft = T extends AtomicObject ? T : T extends ReadonlyMap ? DraftMap : T extends ReadonlySet ? DraftSet : T extends ReadonlyArray ? IsTuple extends true ? WritableDraft : DraftArray : T extends object ? WritableDraft : T; /** * Type-cast from a regular type to its draft type. * Use this if you want to assign a regular value wholesale instead of mutating an existing one. */ export declare function toDraft(obj: T): Draft; export declare type NoReturn = void | undefined; export {};