immer.d.ts
This file
Path
src/immer.d.ts
209 lines · 6.8 KiB · TypeScript
Last changed by
fix(ts): base type of curried produceraleclarsonbefore adoptionb357e54
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): base type of curried produceraleclarsonbefore adoptionb357e54
lines 99–102why, one step back99 ): <T>(100 base: (Draft<T> extends Draft<Base> ? T : Base) | undefined,101 ...rest: Rest102 ) => Produced<Base, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 103–106why, one step back103104 /** Curried producer with no default value */105 <Base = any, Rest extends any[] = [], Return = void>(106 recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Returnfix(ts): base type of curried produceraleclarsonbefore adoptionb357e54
lines 107–110why, one step back107 ): <T>(108 base: Draft<T> extends Draft<Base> ? T : Base,109 ...rest: Rest110 ) => Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 111–115why, one step back111}112113export const produce: IProduce114export default produce115fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 116–121why, one step back116/** Use a class type for `nothing` so its type is unique */117declare class Nothing {118 // This lets us do `Exclude<T, Nothing>`119 private _: any120}121fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 122–124why, one step back122/**123 * The sentinel value returned by producers to replace the draft with undefined.124 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 125why, one step back125export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 126–209why, one step back126127/**128 * Pass true to automatically freeze all copies created by Immer.129 *130 * By default, auto-freezing is disabled in production.131 */132export function setAutoFreeze(autoFreeze: boolean): void133134/**135 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is136 * always faster than using ES5 proxies.137 *138 * By default, feature detection is used, so calling this is rarely necessary.139 */140export function setUseProxies(useProxies: boolean): void141142/**143 * Apply an array of Immer patches to the first argument.144 *145 * This function is a producer, which means copy-on-write is in effect.146 */147export function applyPatches<S>(base: S, patches: Patch[]): S148149export function original<T>(value: T): T | void150151export function isDraft(value: any): boolean152153export class Immer {154 constructor(config: {155 useProxies?: boolean156 autoFreeze?: boolean157 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void158 onDelete?: (state: ImmerState, prop: keyof any) => void159 onCopy?: (state: ImmerState) => void160 })161 /**162 * The `produce` function takes a value and a "recipe function" (whose163 * return value often depends on the base state). The recipe function is164 * free to mutate its first argument however it wants. All mutations are165 * only ever applied to a __copy__ of the base state.166 *167 * Pass only a function to create a "curried producer" which relieves you168 * from passing the recipe function every time.169 *170 * Only plain objects and arrays are made mutable. All other objects are171 * considered uncopyable.172 *173 * Note: This function is __bound__ to its `Immer` instance.174 *175 * @param {any} base - the initial state176 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified177 * @param {Function} patchListener - optional function that will be called with all the patches produced here178 * @returns {any} a new state, or the initial state if nothing was modified179 */180 produce: IProduce181 /**182 * When true, `produce` will freeze the copies it creates.183 */184 readonly autoFreeze: boolean185 /**186 * When true, drafts are ES2015 proxies.187 */188 readonly useProxies: boolean189 /**190 * Pass true to automatically freeze all copies created by Immer.191 *192 * By default, auto-freezing is disabled in production.193 */194 setAutoFreeze(autoFreeze: boolean): void195 /**196 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is197 * always faster than using ES5 proxies.198 *199 * By default, feature detection is used, so calling this is rarely necessary.200 */201 setUseProxies(useProxies: boolean): void202}203204export interface ImmerState<T = any> {205 parent?: ImmerState206 base: T207 copy: T208 assigned: {[prop: string]: boolean}209}