immer.d.ts
This file
Path
src/immer.d.ts
203 lines · 6.6 KiB · TypeScript
Last changed by
fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
History of this fileTake it
fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 1–14why, 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 | String14fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 15–16why, one step back15type ArrayMethod = Exclude<keyof [], number>16type Indices<T> = Exclude<keyof T, ArrayMethod>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 17why, one step back17fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 18–19why, one step back18export type DraftArray<T extends ReadonlyArray<any>> = Array<19 {[P in Indices<T>]: Draft<T[P]>}[Indices<T>]fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 20–21why, one step back20>21fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 22–23why, one step back22export type DraftTuple<T extends ReadonlyArray<any>> = {23 [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : neverfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 24–25why, one step back24}25fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 26–27why, one step back26export type Draft<T> = T extends never[]27 ? Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 28why, one step back28 : T extends ReadonlyArray<any>fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 29–33why, one step back29 ? T[number][] extends T30 ? DraftArray<T>31 : DraftTuple<T>32 : T extends AtomicObject33 ? Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 34why, one step back34 : T extends objectfix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
line 35why, one step back35 ? {-readonly [P in keyof T]: Draft<T[P]>}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 36–45why, one step back36 : T3738export interface Patch {39 op: "replace" | "remove" | "add"40 path: (string | number)[]41 value?: any42}4344export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void45fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
lines 46–54why, one step back46/** Includes 1 when `void` or `undefined` exists in type `T` */47type HasVoidLike<T> = (void extends T ? 1 : 0) | (undefined extends T ? 1 : 0)4849/** Includes 1 when type `T` is `void` or `undefined` (or both) */50type IsVoidLike<T> =51 | (T extends void ? 1 : 0)52 | (T extends undefined ? 1 : 0)53 | (T extends void | undefined ? 1 : 0)54fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 55–58why, one step back55/** Converts `nothing` into `undefined` */56type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T5758/** The inferred return type of `produce` */fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
lines 59–60why, one step back59type Produced<Base, Return> = 1 extends HasVoidLike<Return>60 ? 1 extends IsVoidLike<Return>fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 61–64why, one step back61 ? Base62 : Base | FromNothing<Exclude<Return, void>>63 : FromNothing<Return>64fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 65–84why, one step back65export interface IProduce {66 /**67 * The `produce` function takes a value and a "recipe function" (whose68 * return value often depends on the base state). The recipe function is69 * free to mutate its first argument however it wants. All mutations are70 * only ever applied to a __copy__ of the base state.71 *72 * Pass only a function to create a "curried producer" which relieves you73 * from passing the recipe function every time.74 *75 * Only plain objects and arrays are made mutable. All other objects are76 * considered uncopyable.77 *78 * Note: This function is __bound__ to its `Immer` instance.79 *80 * @param {any} base - the initial state81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified82 * @param {Function} patchListener - optional function that will be called with all the patches produced here83 * @returns {any} a new state, or the initial state if nothing was modified84 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 85–87why, one step back85 <Base, Proxy = Draft<Base>, Return = void>(86 base: Base,87 recipe: (this: Proxy, draft: Proxy) => Return,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 88why, one step back88 listener?: PatchListenerfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 89why, one step back89 ): Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 90why, one step back90fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 91why, one step back91 /** Curried producer with a default value */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 92why, one step back92 <Default = any, Base = Default, Rest extends any[] = [], Return = void>(fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 93why, one step back93 recipe: (fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 94–97why, one step back94 this: Draft<Base>,95 draft: Draft<Base>,96 ...rest: Rest97 ) => Return,fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 98why, one step back98 defaultBase: Defaultfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 99why, one step back99 ): (base: Base | undefined, ...rest: Rest) => Produced<Base, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 100–104why, one step back100101 /** Curried producer with no default value */102 <Base = any, Rest extends any[] = [], Return = void>(103 recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return104 ): (base: Base, ...rest: Rest) => Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 105–109why, one step back105}106107export const produce: IProduce108export default produce109fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 110–115why, one step back110/** Use a class type for `nothing` so its type is unique */111declare class Nothing {112 // This lets us do `Exclude<T, Nothing>`113 private _: any114}115fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 116–118why, one step back116/**117 * The sentinel value returned by producers to replace the draft with undefined.118 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 119why, one step back119export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 120–203why, one step back120121/**122 * Pass true to automatically freeze all copies created by Immer.123 *124 * By default, auto-freezing is disabled in production.125 */126export function setAutoFreeze(autoFreeze: boolean): void127128/**129 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is130 * always faster than using ES5 proxies.131 *132 * By default, feature detection is used, so calling this is rarely necessary.133 */134export function setUseProxies(useProxies: boolean): void135136/**137 * Apply an array of Immer patches to the first argument.138 *139 * This function is a producer, which means copy-on-write is in effect.140 */141export function applyPatches<S>(base: S, patches: Patch[]): S142143export function original<T>(value: T): T | void144145export function isDraft(value: any): boolean146147export class Immer {148 constructor(config: {149 useProxies?: boolean150 autoFreeze?: boolean151 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void152 onDelete?: (state: ImmerState, prop: keyof any) => void153 onCopy?: (state: ImmerState) => void154 })155 /**156 * The `produce` function takes a value and a "recipe function" (whose157 * return value often depends on the base state). The recipe function is158 * free to mutate its first argument however it wants. All mutations are159 * only ever applied to a __copy__ of the base state.160 *161 * Pass only a function to create a "curried producer" which relieves you162 * from passing the recipe function every time.163 *164 * Only plain objects and arrays are made mutable. All other objects are165 * considered uncopyable.166 *167 * Note: This function is __bound__ to its `Immer` instance.168 *169 * @param {any} base - the initial state170 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified171 * @param {Function} patchListener - optional function that will be called with all the patches produced here172 * @returns {any} a new state, or the initial state if nothing was modified173 */174 produce: IProduce175 /**176 * When true, `produce` will freeze the copies it creates.177 */178 readonly autoFreeze: boolean179 /**180 * When true, drafts are ES2015 proxies.181 */182 readonly useProxies: boolean183 /**184 * Pass true to automatically freeze all copies created by Immer.185 *186 * By default, auto-freezing is disabled in production.187 */188 setAutoFreeze(autoFreeze: boolean): void189 /**190 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is191 * always faster than using ES5 proxies.192 *193 * By default, feature detection is used, so calling this is rarely necessary.194 */195 setUseProxies(useProxies: boolean): void196}197198export interface ImmerState<T = any> {199 parent?: ImmerState200 base: T201 copy: T202 assigned: {[prop: string]: boolean}203}