index.d.ts
This file
Path
src/index.d.ts
115 lines · 3.6 KiB · TypeScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 1–115why, 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 * Immer takes a state, and runs a function against it.59 * That function can freely mutate the state, as it will create copies-on-write.60 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned.61 *62 * If the first argument is a function, this is interpreted as the recipe, and will create a curried function that will execute the recipe63 * any time it is called with the current state.64 *65 * @param currentState - the state to start with66 * @param recipe - function that receives a proxy of the current state as first argument and which can be freely modified67 * @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 undefined68 * @returns The next state: a new state, or the current state if nothing was modified69 */70 <S = any, R = never>(71 currentState: S,72 recipe: (this: Draft<S>, draftState: Draft<S>) => void | R,73 listener?: PatchListener74 ): R7576 /** Curried producer with an initial state */77 <S = any, R = never>(78 recipe: (this: Draft<S>, draftState: Draft<S>) => void | R,79 initialState: S80 ): (currentState: S | undefined) => R8182 /** Curried producer with no initial state */83 <S = any, R = never, Args extends any[] = any[]>(84 recipe: (85 this: Draft<S>,86 draftState: Draft<S>,87 ...extraArgs: Args88 ) => void | R89 ): (currentState: S, ...extraArgs: Args) => R90}9192export const produce: IProduce93export default produce9495export const nothing: undefined9697/**98 * Automatically freezes any state trees generated by immer.99 * This protects against accidental modifications of the state tree outside of an immer function.100 * This comes with a performance impact, so it is recommended to disable this option in production.101 * It is by default enabled.102 */103export function setAutoFreeze(autoFreeze: boolean): void104105/**106 * Manually override whether proxies should be used.107 * By default done by using feature detection108 */109export function setUseProxies(useProxies: boolean): void110111export function applyPatches<S>(state: S, patches: Patch[]): S112113export function original<T>(value: T): T | void114115export function isDraft(value: any): boolean