Skip to content
Myr

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 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–217why, 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[]): 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}