Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

249 lines · 8.0 KiB · TypeScript

Last changed by

chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae

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>

chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae

line 52why, one step back
52

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

lines 53–73why, one step back
53export interface IProduce {
54 /**55 * The `produce` function takes a value and a "recipe function" (whose56 * return value often depends on the base state). The recipe function is57 * free to mutate its first argument however it wants. All mutations are58 * only ever applied to a __copy__ of the base state.59 *60 * Pass only a function to create a "curried producer" which relieves you61 * from passing the recipe function every time.62 *63 * Only plain objects and arrays are made mutable. All other objects are64 * considered uncopyable.65 *66 * Note: This function is __bound__ to its `Immer` instance.67 *68 * @param {any} base - the initial state69 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified70 * @param {Function} patchListener - optional function that will be called with all the patches produced here71 * @returns {any} a new state, or the initial state if nothing was modified72 */73

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

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

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

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

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

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

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

lines 84–87why, one step back
84 ) => Produced<S, ReturnType<Recipe>>
85 // ^ by making the returned type generic, the actual type of the passed in object is preferred86 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe87 // 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 88why, one step back
88

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

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

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

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

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

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

feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3

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

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

lines 101–104why, one step back
101
102 /** Normal producer */103 <Base, D = Draft<Base>, Return = void>(104 base: Base,

chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae

line 105why, one step back
105 recipe: (draft: D) => Return,

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

lines 106–107why, one step back
106 listener?: PatchListener
107 ): Produced<Base, Return>

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

lines 108–112why, one step back
108}
109110export const produce: IProduce111export default produce112

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 113–118why, one step back
113/** Use a class type for `nothing` so its type is unique */
114declare class Nothing {115 // This lets us do `Exclude<T, Nothing>`116 private _: any117}118

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

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

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

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

lines 135–154why, one step back
135 * Pass true to automatically freeze all copies created by Immer.
136 *137 * By default, auto-freezing is disabled in production.138 */139export function setAutoFreeze(autoFreeze: boolean): void140141/**142 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is143 * always faster than using ES5 proxies.144 *145 * By default, feature detection is used, so calling this is rarely necessary.146 */147export function setUseProxies(useProxies: boolean): void148149/**150 * Apply an array of Immer patches to the first argument.151 *152 * This function is a producer, which means copy-on-write is in effect.153 */154export function applyPatches<S>(base: S, patches: Patch[]): S

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 155–170why, one step back
155
156/**157 * Create an Immer draft from the given base state, which may be a draft itself.158 * The draft can be modified until you finalize it with the `finishDraft` function.159 */160export function createDraft<T>(base: T): Draft<T>161162/**163 * Finalize an Immer draft from a `createDraft` call, returning the base state164 * (if no changes were made) or a modified copy. The draft must *not* be165 * mutated afterwards.166 *167 * Pass a function as the 2nd argument to generate Immer patches based on the168 * changes that were made.169 */170export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>

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

line 171why, one step back
171

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

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

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

lines 185–190why, one step back
185 onAssign?: (
186 state: ImmerState,187 prop: string | number,188 value: unknown189 ) => void190 onDelete?: (state: ImmerState, prop: string | number) => void

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

lines 191–239why, one step back
191 onCopy?: (state: ImmerState) => void
192 })193 /**194 * The `produce` function takes a value and a "recipe function" (whose195 * return value often depends on the base state). The recipe function is196 * free to mutate its first argument however it wants. All mutations are197 * only ever applied to a __copy__ of the base state.198 *199 * Pass only a function to create a "curried producer" which relieves you200 * from passing the recipe function every time.201 *202 * Only plain objects and arrays are made mutable. All other objects are203 * considered uncopyable.204 *205 * Note: This function is __bound__ to its `Immer` instance.206 *207 * @param {any} base - the initial state208 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified209 * @param {Function} patchListener - optional function that will be called with all the patches produced here210 * @returns {any} a new state, or the initial state if nothing was modified211 */212 produce: IProduce213 /**214 * When true, `produce` will freeze the copies it creates.215 */216 readonly autoFreeze: boolean217 /**218 * When true, drafts are ES2015 proxies.219 */220 readonly useProxies: boolean221 /**222 * Pass true to automatically freeze all copies created by Immer.223 *224 * By default, auto-freezing is disabled in production.225 */226 setAutoFreeze(autoFreeze: boolean): void227 /**228 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is229 * always faster than using ES5 proxies.230 *231 * By default, feature detection is used, so calling this is rarely necessary.232 */233 setUseProxies(useProxies: boolean): void234}235236export interface ImmerState<T = any> {237 parent?: ImmerState238 base: T239 copy: T

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

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

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

line 241why, one step back
241}

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

lines 242–249why, one step back
242
243// Backward compatibility with --target es5244declare global {245 interface Set<T> {}246 interface Map<K, V> {}247 interface WeakSet<T> {}248 interface WeakMap<K extends object, V> {}249}