immer.d.ts
This file
Path
src/immer.d.ts
198 lines · 6.4 KiB · TypeScript
Last changed by
fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
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): improve IProducealeclarsonbefore adoption8666d32
lines 91–92why, one step back91 /** Curried producer */92 <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–99why, one step back94 this: Draft<Base>,95 draft: Draft<Base>,96 ...rest: Rest97 ) => Return,98 defaultBase?: Default99 ): (base: Base | undefined, ...rest: Rest) => Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 100–104why, one step back100}101102export const produce: IProduce103export default produce104fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 105–110why, one step back105/** Use a class type for `nothing` so its type is unique */106declare class Nothing {107 // This lets us do `Exclude<T, Nothing>`108 private _: any109}110fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 111–113why, one step back111/**112 * The sentinel value returned by producers to replace the draft with undefined.113 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 114why, one step back114export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 115–198why, one step back115116/**117 * Pass true to automatically freeze all copies created by Immer.118 *119 * By default, auto-freezing is disabled in production.120 */121export function setAutoFreeze(autoFreeze: boolean): void122123/**124 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is125 * always faster than using ES5 proxies.126 *127 * By default, feature detection is used, so calling this is rarely necessary.128 */129export function setUseProxies(useProxies: boolean): void130131/**132 * Apply an array of Immer patches to the first argument.133 *134 * This function is a producer, which means copy-on-write is in effect.135 */136export function applyPatches<S>(base: S, patches: Patch[]): S137138export function original<T>(value: T): T | void139140export function isDraft(value: any): boolean141142export class Immer {143 constructor(config: {144 useProxies?: boolean145 autoFreeze?: boolean146 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void147 onDelete?: (state: ImmerState, prop: keyof any) => void148 onCopy?: (state: ImmerState) => void149 })150 /**151 * The `produce` function takes a value and a "recipe function" (whose152 * return value often depends on the base state). The recipe function is153 * free to mutate its first argument however it wants. All mutations are154 * only ever applied to a __copy__ of the base state.155 *156 * Pass only a function to create a "curried producer" which relieves you157 * from passing the recipe function every time.158 *159 * Only plain objects and arrays are made mutable. All other objects are160 * considered uncopyable.161 *162 * Note: This function is __bound__ to its `Immer` instance.163 *164 * @param {any} base - the initial state165 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified166 * @param {Function} patchListener - optional function that will be called with all the patches produced here167 * @returns {any} a new state, or the initial state if nothing was modified168 */169 produce: IProduce170 /**171 * When true, `produce` will freeze the copies it creates.172 */173 readonly autoFreeze: boolean174 /**175 * When true, drafts are ES2015 proxies.176 */177 readonly useProxies: boolean178 /**179 * Pass true to automatically freeze all copies created by Immer.180 *181 * By default, auto-freezing is disabled in production.182 */183 setAutoFreeze(autoFreeze: boolean): void184 /**185 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is186 * always faster than using ES5 proxies.187 *188 * By default, feature detection is used, so calling this is rarely necessary.189 */190 setUseProxies(useProxies: boolean): void191}192193export interface ImmerState<T = any> {194 parent?: ImmerState195 base: T196 copy: T197 assigned: {[prop: string]: boolean}198}