immer.d.ts
This file
Path
src/immer.d.ts
248 lines · 8.1 KiB · TypeScript
Last changed by
fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
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): improve IProducealeclarsonbefore adoption8666d32
line 46why, one step back46/** Converts `nothing` into `undefined` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 47why, one step back47type FromNothing<T> = T extends Nothing ? undefined : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 48–49why, one step back4849/** The inferred return type of `produce` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 50–54why, one step back50export type Produced<Base, Return> = Return extends void51 ? Base52 : Return extends Promise<infer Result>53 ? Promise<Result extends void ? Base : FromNothing<Result>>54 : FromNothing<Return>fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
lines 55–69why, one step back5556type ImmutableTuple<T extends ReadonlyArray<any>> = {57 readonly [P in keyof T]: Immutable<T[P]>58}5960/** Convert a mutable type into a readonly type */61export type Immutable<T> = T extends object62 ? T extends AtomicObject63 ? T64 : T extends ReadonlyArray<any>65 ? Array<T[number]> extends T66 ? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]67 : ImmutableTuple<T>68 : {readonly [P in keyof T]: Immutable<T[P]>}69 : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 70why, one step back70fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 71–90why, one step back71export interface IProduce {72 /**73 * The `produce` function takes a value and a "recipe function" (whose74 * return value often depends on the base state). The recipe function is75 * free to mutate its first argument however it wants. All mutations are76 * only ever applied to a __copy__ of the base state.77 *78 * Pass only a function to create a "curried producer" which relieves you79 * from passing the recipe function every time.80 *81 * Only plain objects and arrays are made mutable. All other objects are82 * considered uncopyable.83 *84 * Note: This function is __bound__ to its `Immer` instance.85 *86 * @param {any} base - the initial state87 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified88 * @param {Function} patchListener - optional function that will be called with all the patches produced here89 * @returns {any} a new state, or the initial state if nothing was modified90 */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 91why, one step back91 <T = any, Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 92–93why, one step back92 base: T,93 recipe: (this: D, draft: D) => Return,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 94why, one step back94 listener?: PatchListenerfix(ts): use base type as default produce() return typealeclarsonbefore adoption3aa1c4a
line 95why, one step back95 ): Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 96why, one step back96fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 97why, one step back97 /** Curried producer with a default value */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 98why, one step back98 <T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 99–101why, one step back99 recipe: (this: D, draft: D, ...rest: Rest) => Return,100 defaultBase: T101 ): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 102–103why, one step back102103 /** Curried producer with no default value */fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 104–106why, one step back104 <T = any, Rest extends any[] = [], Return = void>(105 recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return106 ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 107–111why, one step back107}108109export const produce: IProduce110export default produce111fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 112–117why, one step back112/** Use a class type for `nothing` so its type is unique */113declare class Nothing {114 // This lets us do `Exclude<T, Nothing>`115 private _: any116}117fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 118–120why, one step back118/**119 * The sentinel value returned by producers to replace the draft with undefined.120 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 121why, one step back121export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 122–123why, one step back122123/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 124–133why, one step back124 * To let Immer treat your class instances as plain immutable objects125 * (albeit with a custom prototype), you must define either an instance property126 * or a static property on each of your custom classes.127 *128 * Otherwise, your class instance will never be drafted, which means it won't be129 * safe to mutate in a produce callback.130 */131export const immerable: unique symbol132133/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 134–153why, one step back134 * Pass true to automatically freeze all copies created by Immer.135 *136 * By default, auto-freezing is disabled in production.137 */138export function setAutoFreeze(autoFreeze: boolean): void139140/**141 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is142 * always faster than using ES5 proxies.143 *144 * By default, feature detection is used, so calling this is rarely necessary.145 */146export function setUseProxies(useProxies: boolean): void147148/**149 * Apply an array of Immer patches to the first argument.150 *151 * This function is a producer, which means copy-on-write is in effect.152 */153export function applyPatches<S>(base: S, patches: Patch[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 154–169why, one step back154155/**156 * Create an Immer draft from the given base state, which may be a draft itself.157 * The draft can be modified until you finalize it with the `finishDraft` function.158 */159export function createDraft<T>(base: T): Draft<T>160161/**162 * Finalize an Immer draft from a `createDraft` call, returning the base state163 * (if no changes were made) or a modified copy. The draft must *not* be164 * mutated afterwards.165 *166 * Pass a function as the 2nd argument to generate Immer patches based on the167 * changes that were made.168 */169export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 170why, one step back170fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 171why, one step back171/** Get the underlying object that is represented by the given draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 172–173why, one step back172export function original<T>(value: T): T | void173feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 174why, one step back174/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 175why, one step back175export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 176–178why, one step back176177/** Returns true if the given value can be drafted by Immer */178export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 179–183why, one step back179180export class Immer {181 constructor(config: {182 useProxies?: boolean183 autoFreeze?: booleanfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
lines 184–189why, one step back184 onAssign?: (185 state: ImmerState,186 prop: string | number,187 value: unknown188 ) => void189 onDelete?: (state: ImmerState, prop: string | number) => voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 190–238why, one step back190 onCopy?: (state: ImmerState) => void191 })192 /**193 * The `produce` function takes a value and a "recipe function" (whose194 * return value often depends on the base state). The recipe function is195 * free to mutate its first argument however it wants. All mutations are196 * only ever applied to a __copy__ of the base state.197 *198 * Pass only a function to create a "curried producer" which relieves you199 * from passing the recipe function every time.200 *201 * Only plain objects and arrays are made mutable. All other objects are202 * considered uncopyable.203 *204 * Note: This function is __bound__ to its `Immer` instance.205 *206 * @param {any} base - the initial state207 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified208 * @param {Function} patchListener - optional function that will be called with all the patches produced here209 * @returns {any} a new state, or the initial state if nothing was modified210 */211 produce: IProduce212 /**213 * When true, `produce` will freeze the copies it creates.214 */215 readonly autoFreeze: boolean216 /**217 * When true, drafts are ES2015 proxies.218 */219 readonly useProxies: boolean220 /**221 * Pass true to automatically freeze all copies created by Immer.222 *223 * By default, auto-freezing is disabled in production.224 */225 setAutoFreeze(autoFreeze: boolean): void226 /**227 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is228 * always faster than using ES5 proxies.229 *230 * By default, feature detection is used, so calling this is rarely necessary.231 */232 setUseProxies(useProxies: boolean): void233}234235export interface ImmerState<T = any> {236 parent?: ImmerState237 base: T238 copy: Tfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 239why, one step back239 assigned: {[prop: string]: boolean; [index: number]: boolean}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 240why, one step back240}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 241–248why, one step back241242// Backward compatibility with --target es5243declare global {244 interface Set<T> {}245 interface Map<K, V> {}246 interface WeakSet<T> {}247 interface WeakMap<K extends object, V> {}248}