Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

248 lines · 8.0 KiB · TypeScript

Last changed by

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

History of this file

Take it

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

lines 1–7why, one step back
1type Tail<T extends any[]> = ((...t: T) => any) extends ((
2 _: any,3 ...tail: infer TT4) => any)5 ? TT6 : []7

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

lines 8–21why, one step back
8/** Object types that should never be mapped */
9type AtomicObject =10 | Function11 | Map<any, any>12 | WeakMap<any, any>13 | Set<any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp18 | Boolean19 | Number20 | String21

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

lines 22–26why, one step back
22export type Draft<T> = T extends AtomicObject
23 ? T24 : T extends object25 ? {-readonly [K in keyof T]: Draft<T[K]>}26 : T // mostly: unknown & any

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

line 27why, one step back
27

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

lines 28–29why, one step back
28/** Convert a mutable type into a readonly type */
29export type Immutable<T> = T extends AtomicObject

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

line 30why, one step back
30 ? T

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

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

refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6

line 32why, one step back
32 ? {readonly [K in keyof T]: Immutable<T[K]>}

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

lines 33–42why, one step back
33 : T
3435export interface Patch {36 op: "replace" | "remove" | "add"37 path: (string | number)[]38 value?: any39}4041export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void42

fix(ts): improve IProducealeclarsonbefore adoption8666d32

line 43why, one step back
43/** Converts `nothing` into `undefined` */

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 44why, one step back
44type FromNothing<T> = T extends Nothing ? undefined : T

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 45–46why, one step back
45
46/** The inferred return type of `produce` */

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 47–51why, one step back
47export type Produced<Base, Return> = Return extends void
48 ? Base49 : Return extends Promise<infer Result>50 ? Promise<Result extends void ? Base : FromNothing<Result>>51 : FromNothing<Return>

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

lines 52–72why, one step back
52export interface IProduce {
53 /**54 * The `produce` function takes a value and a "recipe function" (whose55 * return value often depends on the base state). The recipe function is56 * free to mutate its first argument however it wants. All mutations are57 * only ever applied to a __copy__ of the base state.58 *59 * Pass only a function to create a "curried producer" which relieves you60 * from passing the recipe function every time.61 *62 * Only plain objects and arrays are made mutable. All other objects are63 * considered uncopyable.64 *65 * Note: This function is __bound__ to its `Immer` instance.66 *67 * @param {any} base - the initial state68 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified69 * @param {Function} patchListener - optional function that will be called with all the patches produced here70 * @returns {any} a new state, or the initial state if nothing was modified71 */72

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

lines 73–79why, one step back
73 /** Curried producer */
74 <75 Recipe extends (...args: any[]) => any,76 Params extends any[] = Parameters<Recipe>,77 T = Params[0]78 >(79 recipe: Recipe

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

lines 80–81why, one step back
80 ): <S extends Immutable<T>>(
81 state: S,

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

line 82why, one step back
82 ...rest: Tail<Params>

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

lines 83–86why, one step back
83 ) => Produced<S, ReturnType<Recipe>>
84 // ^ by making the returned type generic, the actual type of the passed in object is preferred85 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe86 // Note: the type of S is the widened version of T, so it can have more props than T, but that is technically actually correct!

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

line 87why, one step back
87

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

lines 88–94why, one step back
88 /** Curried producer with initial state */
89 <90 Recipe extends (...args: any[]) => any,91 Params extends any[] = Parameters<Recipe>,92 T = Params[0]93 >(94 recipe: Recipe,

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

lines 95–97why, one step back
95 initialState: Immutable<T>
96 ): <S extends Immutable<T>>(97 state?: S,

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

line 98why, one step back
98 ...rest: Tail<Params>

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

line 99why, one step back
99 ) => Produced<S, ReturnType<Recipe>>

feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57

lines 100–106why, one step back
100
101 /** Normal producer */102 <Base, D = Draft<Base>, Return = void>(103 base: Base,104 recipe: (this: D, draft: D) => Return,105 listener?: PatchListener106 ): Produced<Base, Return>

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

lines 107–111why, one step back
107}
108109export const produce: IProduce110export default produce111

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

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

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 122–123why, one step back
122
123/**

feat: limited class supportaleclarsonbefore adoption9877d64

lines 124–133why, one step back
124 * To let Immer treat your class instances as plain immutable objects
125 * (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 back
134 * 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[]): S

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 154–169why, one step back
154
155/**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 back
170

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

line 171why, one step back
171/** 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 back
172export function original<T>(value: T): T | void
173

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

lines 179–183why, one step back
179
180export class Immer {181 constructor(config: {182 useProxies?: boolean183 autoFreeze?: boolean

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

lines 184–189why, one step back
184 onAssign?: (
185 state: ImmerState,186 prop: string | number,187 value: unknown188 ) => void189 onDelete?: (state: ImmerState, prop: string | number) => void

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

lines 190–238why, one step back
190 onCopy?: (state: ImmerState) => void
191 })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: T

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

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

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

line 240why, one step back
240}

fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9

lines 241–248why, one step back
241
242// 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}