Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

245 lines · 7.7 KiB · TypeScript

Last changed by

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

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–83why, 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: Recipe80 ): (81 state: Immutable<T>,82 ...rest: Tail<Params>83 ) => Produced<Immutable<T>, ReturnType<Recipe>>

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

line 84why, one step back
84

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

lines 85–103why, one step back
85 /** Curried producer with initial state */
86 <87 Recipe extends (...args: any[]) => any,88 Params extends any[] = Parameters<Recipe>,89 T = Params[0]90 >(91 recipe: Recipe,92 initialState: T93 ): (94 state?: Immutable<T>,95 ...rest: Tail<Params>96 ) => Produced<Immutable<T>, ReturnType<Recipe>>9798 /** Normal producer */99 <Base, D = Draft<Base>, Return = void>(100 base: Base,101 recipe: (this: D, draft: D) => Return,102 listener?: PatchListener103 ): Produced<Base, Return>

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

lines 104–108why, one step back
104}
105106export const produce: IProduce107export default produce108

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 109–114why, one step back
109/** Use a class type for `nothing` so its type is unique */
110declare class Nothing {111 // This lets us do `Exclude<T, Nothing>`112 private _: any113}114

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

lines 115–117why, one step back
115/**
116 * The sentinel value returned by producers to replace the draft with undefined.117 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 119–120why, one step back
119
120/**

feat: limited class supportaleclarsonbefore adoption9877d64

lines 121–130why, one step back
121 * To let Immer treat your class instances as plain immutable objects
122 * (albeit with a custom prototype), you must define either an instance property123 * or a static property on each of your custom classes.124 *125 * Otherwise, your class instance will never be drafted, which means it won't be126 * safe to mutate in a produce callback.127 */128export const immerable: unique symbol129130/**

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

lines 131–150why, one step back
131 * Pass true to automatically freeze all copies created by Immer.
132 *133 * By default, auto-freezing is disabled in production.134 */135export function setAutoFreeze(autoFreeze: boolean): void136137/**138 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is139 * always faster than using ES5 proxies.140 *141 * By default, feature detection is used, so calling this is rarely necessary.142 */143export function setUseProxies(useProxies: boolean): void144145/**146 * Apply an array of Immer patches to the first argument.147 *148 * This function is a producer, which means copy-on-write is in effect.149 */150export function applyPatches<S>(base: S, patches: Patch[]): S

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 151–166why, one step back
151
152/**153 * Create an Immer draft from the given base state, which may be a draft itself.154 * The draft can be modified until you finalize it with the `finishDraft` function.155 */156export function createDraft<T>(base: T): Draft<T>157158/**159 * Finalize an Immer draft from a `createDraft` call, returning the base state160 * (if no changes were made) or a modified copy. The draft must *not* be161 * mutated afterwards.162 *163 * Pass a function as the 2nd argument to generate Immer patches based on the164 * changes that were made.165 */166export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>

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

line 167why, one step back
167

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

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

lines 176–180why, one step back
176
177export class Immer {178 constructor(config: {179 useProxies?: boolean180 autoFreeze?: boolean

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

lines 181–186why, one step back
181 onAssign?: (
182 state: ImmerState,183 prop: string | number,184 value: unknown185 ) => void186 onDelete?: (state: ImmerState, prop: string | number) => void

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

lines 187–235why, one step back
187 onCopy?: (state: ImmerState) => void
188 })189 /**190 * The `produce` function takes a value and a "recipe function" (whose191 * return value often depends on the base state). The recipe function is192 * free to mutate its first argument however it wants. All mutations are193 * only ever applied to a __copy__ of the base state.194 *195 * Pass only a function to create a "curried producer" which relieves you196 * from passing the recipe function every time.197 *198 * Only plain objects and arrays are made mutable. All other objects are199 * considered uncopyable.200 *201 * Note: This function is __bound__ to its `Immer` instance.202 *203 * @param {any} base - the initial state204 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified205 * @param {Function} patchListener - optional function that will be called with all the patches produced here206 * @returns {any} a new state, or the initial state if nothing was modified207 */208 produce: IProduce209 /**210 * When true, `produce` will freeze the copies it creates.211 */212 readonly autoFreeze: boolean213 /**214 * When true, drafts are ES2015 proxies.215 */216 readonly useProxies: boolean217 /**218 * Pass true to automatically freeze all copies created by Immer.219 *220 * By default, auto-freezing is disabled in production.221 */222 setAutoFreeze(autoFreeze: boolean): void223 /**224 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is225 * always faster than using ES5 proxies.226 *227 * By default, feature detection is used, so calling this is rarely necessary.228 */229 setUseProxies(useProxies: boolean): void230}231232export interface ImmerState<T = any> {233 parent?: ImmerState234 base: T235 copy: T

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

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

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

line 237why, one step back
237}

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

lines 238–245why, one step back
238
239// Backward compatibility with --target es5240declare global {241 interface Set<T> {}242 interface Map<K, V> {}243 interface WeakSet<T> {}244 interface WeakMap<K extends object, V> {}245}