immer.d.ts
This file
Path
src/immer.d.ts
217 lines · 7.1 KiB · TypeScript
Last changed by
feat: limited class supportaleclarsonbefore adoption9877d64
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): produce() return typealeclarsonbefore adoptionb52b45e
line 46why, one step back46type IsVoidLike<T> = T extends void | undefined ? 1 : 0fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
line 47why, one step back47fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 48–51why, one step back48/** Converts `nothing` into `undefined` */49type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T5051/** The inferred return type of `produce` */fix(ts): produce() return typealeclarsonbefore adoptionb52b45e
lines 52–56why, one step back52export type Produced<T, Return> = IsVoidLike<Return> extends 053 ? FromNothing<Return>54 : IsVoidLike<Return> extends 155 ? T56 : T | FromNothing<Exclude<Return, void>>fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
lines 57–71why, one step back5758type ImmutableTuple<T extends ReadonlyArray<any>> = {59 readonly [P in keyof T]: Immutable<T[P]>60}6162/** Convert a mutable type into a readonly type */63export type Immutable<T> = T extends object64 ? T extends AtomicObject65 ? T66 : T extends ReadonlyArray<any>67 ? Array<T[number]> extends T68 ? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]69 : ImmutableTuple<T>70 : {readonly [P in keyof T]: Immutable<T[P]>}71 : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 72why, one step back72fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 73–92why, one step back73export interface IProduce {74 /**75 * The `produce` function takes a value and a "recipe function" (whose76 * return value often depends on the base state). The recipe function is77 * free to mutate its first argument however it wants. All mutations are78 * only ever applied to a __copy__ of the base state.79 *80 * Pass only a function to create a "curried producer" which relieves you81 * from passing the recipe function every time.82 *83 * Only plain objects and arrays are made mutable. All other objects are84 * considered uncopyable.85 *86 * Note: This function is __bound__ to its `Immer` instance.87 *88 * @param {any} base - the initial state89 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified90 * @param {Function} patchListener - optional function that will be called with all the patches produced here91 * @returns {any} a new state, or the initial state if nothing was modified92 */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 93why, one step back93 <T = any, Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 94–95why, one step back94 base: T,95 recipe: (this: D, draft: D) => Return,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 96why, one step back96 listener?: PatchListenerfix(ts): use base type as default produce() return typealeclarsonbefore adoption3aa1c4a
line 97why, one step back97 ): Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 98why, one step back98fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 99why, one step back99 /** Curried producer with a default value */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 100why, one step back100 <T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 101–103why, one step back101 recipe: (this: D, draft: D, ...rest: Rest) => Return,102 defaultBase: T103 ): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 104–105why, one step back104105 /** Curried producer with no default value */fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 106–108why, one step back106 <T = any, Rest extends any[] = [], Return = void>(107 recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return108 ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 109–113why, one step back109}110111export const produce: IProduce112export default produce113fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 114–119why, one step back114/** Use a class type for `nothing` so its type is unique */115declare class Nothing {116 // This lets us do `Exclude<T, Nothing>`117 private _: any118}119fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 120–122why, one step back120/**121 * The sentinel value returned by producers to replace the draft with undefined.122 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 123why, one step back123export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 124–125why, one step back124125/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 126–135why, one step back126 * To let Immer treat your class instances as plain immutable objects127 * (albeit with a custom prototype), you must define either an instance property128 * or a static property on each of your custom classes.129 *130 * Otherwise, your class instance will never be drafted, which means it won't be131 * safe to mutate in a produce callback.132 */133export const immerable: unique symbol134135/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 136–217why, one step back136 * Pass true to automatically freeze all copies created by Immer.137 *138 * By default, auto-freezing is disabled in production.139 */140export function setAutoFreeze(autoFreeze: boolean): void141142/**143 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is144 * always faster than using ES5 proxies.145 *146 * By default, feature detection is used, so calling this is rarely necessary.147 */148export function setUseProxies(useProxies: boolean): void149150/**151 * Apply an array of Immer patches to the first argument.152 *153 * This function is a producer, which means copy-on-write is in effect.154 */155export function applyPatches<S>(base: S, patches: Patch[]): S156157export function original<T>(value: T): T | void158159export function isDraft(value: any): boolean160161export class Immer {162 constructor(config: {163 useProxies?: boolean164 autoFreeze?: boolean165 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void166 onDelete?: (state: ImmerState, prop: keyof any) => void167 onCopy?: (state: ImmerState) => void168 })169 /**170 * The `produce` function takes a value and a "recipe function" (whose171 * return value often depends on the base state). The recipe function is172 * free to mutate its first argument however it wants. All mutations are173 * only ever applied to a __copy__ of the base state.174 *175 * Pass only a function to create a "curried producer" which relieves you176 * from passing the recipe function every time.177 *178 * Only plain objects and arrays are made mutable. All other objects are179 * considered uncopyable.180 *181 * Note: This function is __bound__ to its `Immer` instance.182 *183 * @param {any} base - the initial state184 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified185 * @param {Function} patchListener - optional function that will be called with all the patches produced here186 * @returns {any} a new state, or the initial state if nothing was modified187 */188 produce: IProduce189 /**190 * When true, `produce` will freeze the copies it creates.191 */192 readonly autoFreeze: boolean193 /**194 * When true, drafts are ES2015 proxies.195 */196 readonly useProxies: boolean197 /**198 * Pass true to automatically freeze all copies created by Immer.199 *200 * By default, auto-freezing is disabled in production.201 */202 setAutoFreeze(autoFreeze: boolean): void203 /**204 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is205 * always faster than using ES5 proxies.206 *207 * By default, feature detection is used, so calling this is rarely necessary.208 */209 setUseProxies(useProxies: boolean): void210}211212export interface ImmerState<T = any> {213 parent?: ImmerState214 base: T215 copy: T216 assigned: {[prop: string]: boolean}217}