Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

242 lines · 8.0 KiB · TypeScript

Last changed by

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

History of this file

Take it

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 1–14why, one step back
1/** 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 | String14

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

lines 15–16why, one step back
15type 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 back
17

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

lines 18–19why, one step back
18export 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 back
20>
21

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

lines 22–23why, one step back
22export type DraftTuple<T extends ReadonlyArray<any>> = {
23 [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 24–25why, one step back
24}
25

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

lines 26–27why, one step back
26export type Draft<T> = T extends never[]
27 ? T

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 28why, one step back
28 : T extends ReadonlyArray<any>

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

lines 29–33why, one step back
29 ? T[number][] extends T
30 ? DraftArray<T>31 : DraftTuple<T>32 : T extends AtomicObject33 ? T

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 34why, one step back
34 : T extends object

fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7

line 35why, one step back
35 ? {-readonly [P in keyof T]: Draft<T[P]>}

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 36–45why, one step back
36 : T
3738export interface Patch {39 op: "replace" | "remove" | "add"40 path: (string | number)[]41 value?: any42}4344export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void45

fix(ts): produce() return typealeclarsonbefore adoptionb52b45e

line 46why, one step back
46type IsVoidLike<T> = T extends void | undefined ? 1 : 0

fix(ts): return undefined from producealeclarsonbefore adoptiond60db14

line 47why, one step back
47

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 48–51why, one step back
48/** 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 back
52export type Produced<T, Return> = IsVoidLike<Return> extends 0
53 ? 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 back
57
58type 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 : T

fix(ts): improve IProducealeclarsonbefore adoption8666d32

line 72why, one step back
72

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 73–92why, one step back
73export 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 back
93 <T = any, Return = void, D = Draft<T>>(

fix(ts): return type of produce()aleclarsonbefore adoption03ec620

lines 94–95why, one step back
94 base: T,
95 recipe: (this: D, draft: D) => Return,

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 96why, one step back
96 listener?: PatchListener

fix(ts): use base type as default produce() return typealeclarsonbefore adoption3aa1c4a

line 97why, one step back
97 ): Produced<T, Return>

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 98why, one step back
98

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

line 99why, one step back
99 /** Curried producer with a default value */

fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af

line 100why, one step back
100 <T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(

fix(ts): return type of produce()aleclarsonbefore adoption03ec620

lines 101–103why, one step back
101 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 back
104
105 /** Curried producer with no default value */

fix(ts): return type of produce()aleclarsonbefore adoption03ec620

lines 106–108why, one step back
106 <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 back
109}
110111export const produce: IProduce112export default produce113

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 114–119why, one step back
114/** 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}119

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 120–122why, one step back
120/**
121 * The sentinel value returned by producers to replace the draft with undefined.122 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

line 123why, one step back
123export const nothing: Nothing

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 124–125why, one step back
124
125/**

feat: limited class supportaleclarsonbefore adoption9877d64

lines 126–135why, one step back
126 * To let Immer treat your class instances as plain immutable objects
127 * (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–156why, one step back
136 * 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[]): S156

types: createDraft/finishDraft functionsMichel Weststratebefore adoptionedda437

line 157why, one step back
157/**

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

lines 158–159why, one step back
158 * Create an Immer draft from the given base state, which may be a draft itself.
159 * The draft can be modified until you finalize it with the `finishDraft` function.

types: createDraft/finishDraft functionsMichel Weststratebefore adoptionedda437

lines 160–163why, one step back
160 */
161export function createDraft<T>(base: T): Draft<T>162163/**

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

lines 164–169why, one step back
164 * Finalize an Immer draft from a `createDraft` call, returning the base state
165 * (if no changes were made) or a modified copy. The draft must *not* be166 * mutated afterwards.167 *168 * Pass a function as the 2nd argument to generate Immer patches based on the169 * changes that were made.

types: createDraft/finishDraft functionsMichel Weststratebefore adoptionedda437

lines 170–172why, one step back
170 */
171export function finishDraft<T>(base: Draft<T>, listener?: PatchListener): T172

fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c

line 173why, one step back
173/** Get the underlying object that is represented by the given draft */

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 174–175why, one step back
174export function original<T>(value: T): T | void
175

feat: export isDraftablealeclarsonbefore adoptionc89d47d

line 176why, one step back
176/** Returns true if the given value is an Immer draft */

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 177why, one step back
177export function isDraft(value: any): boolean

feat: export isDraftablealeclarsonbefore adoptionc89d47d

lines 178–180why, one step back
178
179/** Returns true if the given value can be drafted by Immer */180export function isDraftable(value: any): boolean

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 181–185why, one step back
181
182export class Immer {183 constructor(config: {184 useProxies?: boolean185 autoFreeze?: boolean

fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c

lines 186–191why, one step back
186 onAssign?: (
187 state: ImmerState,188 prop: string | number,189 value: unknown190 ) => void191 onDelete?: (state: ImmerState, prop: string | number) => void

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

lines 192–240why, one step back
192 onCopy?: (state: ImmerState) => void
193 })194 /**195 * The `produce` function takes a value and a "recipe function" (whose196 * return value often depends on the base state). The recipe function is197 * free to mutate its first argument however it wants. All mutations are198 * only ever applied to a __copy__ of the base state.199 *200 * Pass only a function to create a "curried producer" which relieves you201 * from passing the recipe function every time.202 *203 * Only plain objects and arrays are made mutable. All other objects are204 * considered uncopyable.205 *206 * Note: This function is __bound__ to its `Immer` instance.207 *208 * @param {any} base - the initial state209 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified210 * @param {Function} patchListener - optional function that will be called with all the patches produced here211 * @returns {any} a new state, or the initial state if nothing was modified212 */213 produce: IProduce214 /**215 * When true, `produce` will freeze the copies it creates.216 */217 readonly autoFreeze: boolean218 /**219 * When true, drafts are ES2015 proxies.220 */221 readonly useProxies: boolean222 /**223 * Pass true to automatically freeze all copies created by Immer.224 *225 * By default, auto-freezing is disabled in production.226 */227 setAutoFreeze(autoFreeze: boolean): void228 /**229 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is230 * always faster than using ES5 proxies.231 *232 * By default, feature detection is used, so calling this is rarely necessary.233 */234 setUseProxies(useProxies: boolean): void235}236237export interface ImmerState<T = any> {238 parent?: ImmerState239 base: T240 copy: T

fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c

line 241why, one step back
241 assigned: {[prop: string]: boolean; [index: number]: boolean}

fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843

line 242why, one step back
242}