Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

291 lines · 8.9 KiB · TypeScript

Last changed by

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

History of this file

Take it

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

line 1why, one step back
1type Tail<T extends any[]> = ((...t: T) => any) extends ((

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 2–3why, one step back
2 _: any,
3 ...tail: infer TT

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

line 4why, one step back
4) => any)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 5–6why, one step back
5 ? TT
6 : []

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

line 7why, one step back
7

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

lines 8–9why, one step back
8/** Object types that should never be mapped */
9type AtomicObject =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 10–20why, one step back
10 | Function
11 | Map<any, any>12 | WeakMap<any, any>13 | Set<any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp18 | Boolean19 | Number20 | String

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

line 21why, one step back
21

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

line 22why, one step back
22export type Draft<T> = T extends AtomicObject

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 23–26why, one step back
23 ? T
24 : T extends object25 ? {-readonly [K in keyof T]: Draft<T[K]>}26 : T

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 30–33why, one step back
30 ? T
31 : T extends object32 ? {readonly [K in keyof T]: Immutable<T[K]>}33 : T

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

lines 34–35why, one step back
34
35export interface Patch {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 36–38why, one step back
36 op: "replace" | "remove" | "add"
37 path: (string | number)[]38 value?: any

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

lines 39–42why, one step back
39}
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

line 47why, one step back
47export type Produced<Base, Return> = Return extends void

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 48–51why, one step back
48 ? Base
49 : 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

refactor(ts): tweak IProduce typeAlec Larsonbefore adoption8925080

lines 53–71why, one step back
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 */

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

line 72why, one step back
72export interface IProduce {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 73–86why, 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 ): <Base extends Immutable<T>>(81 base: Base,82 ...rest: Tail<Params>83 ) => Produced<Base, 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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 88–99why, 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,95 initialState: Immutable<T>96 ): <Base extends Immutable<T>>(97 base?: Base,98 ...rest: Tail<Params>99 ) => Produced<Base, ReturnType<Recipe>>

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

line 100why, one step back
100

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 101–106why, one step back
101 /** Normal producer */
102 <Base, D = Draft<Base>, Return = void>(103 base: Base,104 recipe: (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

Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda

lines 112–154why, one step back
112/**
113 * Like `produce`, but instead of just returning the new state,114 * a tuple is returned with [nextState, patches, inversePatches]115 *116 * Like produce, this function supports currying117 */118export interface IProduceWithPatches {119 /** Curried producer */120 <121 Recipe extends (...args: any[]) => any,122 Params extends any[] = Parameters<Recipe>,123 T = Params[0]124 >(125 recipe: Recipe126 ): <Base extends Immutable<T>>(127 base: Base,128 ...rest: Tail<Params>129 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]130 // ^ by making the returned type generic, the actual type of the passed in object is preferred131 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe132 // 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!133134 /** Curried producer with initial state */135 <136 Recipe extends (...args: any[]) => any,137 Params extends any[] = Parameters<Recipe>,138 T = Params[0]139 >(140 recipe: Recipe,141 initialState: Immutable<T>142 ): <Base extends Immutable<T>>(143 base?: Base,144 ...rest: Tail<Params>145 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]146147 /** Normal producer */148 <Base, D = Draft<Base>, Return = void>(149 base: Base,150 recipe: (draft: D) => Return151 ): [Produced<Base, Return>, Patch[], Patch[]]152}153export const produceWithPatches: IProduceWithPatches154

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 155–156why, one step back
155/** Use a class type for `nothing` so its type is unique */
156declare class Nothing {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 157–158why, one step back
157 // This lets us do `Exclude<T, Nothing>`
158 private _: any

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 159–160why, one step back
159}
160

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

lines 161–163why, one step back
161/**
162 * The sentinel value returned by producers to replace the draft with undefined.163 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 165–166why, one step back
165
166/**

feat: limited class supportaleclarsonbefore adoption9877d64

lines 167–176why, one step back
167 * To let Immer treat your class instances as plain immutable objects
168 * (albeit with a custom prototype), you must define either an instance property169 * or a static property on each of your custom classes.170 *171 * Otherwise, your class instance will never be drafted, which means it won't be172 * safe to mutate in a produce callback.173 */174export const immerable: unique symbol175176/**

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

lines 177–196why, one step back
177 * Pass true to automatically freeze all copies created by Immer.
178 *179 * By default, auto-freezing is disabled in production.180 */181export function setAutoFreeze(autoFreeze: boolean): void182183/**184 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is185 * always faster than using ES5 proxies.186 *187 * By default, feature detection is used, so calling this is rarely necessary.188 */189export function setUseProxies(useProxies: boolean): void190191/**192 * Apply an array of Immer patches to the first argument.193 *194 * This function is a producer, which means copy-on-write is in effect.195 */196export function applyPatches<S>(base: S, patches: Patch[]): S

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 197–212why, one step back
197
198/**199 * Create an Immer draft from the given base state, which may be a draft itself.200 * The draft can be modified until you finalize it with the `finishDraft` function.201 */202export function createDraft<T>(base: T): Draft<T>203204/**205 * Finalize an Immer draft from a `createDraft` call, returning the base state206 * (if no changes were made) or a modified copy. The draft must *not* be207 * mutated afterwards.208 *209 * Pass a function as the 2nd argument to generate Immer patches based on the210 * changes that were made.211 */212export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>

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

line 213why, one step back
213

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

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

lines 222–223why, one step back
222
223export class Immer {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 224–275why, one step back
224 constructor(config: {
225 useProxies?: boolean226 autoFreeze?: boolean227 onAssign?: (228 state: ImmerState,229 prop: string | number,230 value: unknown231 ) => void232 onDelete?: (state: ImmerState, prop: string | number) => void233 onCopy?: (state: ImmerState) => void234 })235 /**236 * The `produce` function takes a value and a "recipe function" (whose237 * return value often depends on the base state). The recipe function is238 * free to mutate its first argument however it wants. All mutations are239 * only ever applied to a __copy__ of the base state.240 *241 * Pass only a function to create a "curried producer" which relieves you242 * from passing the recipe function every time.243 *244 * Only plain objects and arrays are made mutable. All other objects are245 * considered uncopyable.246 *247 * Note: This function is __bound__ to its `Immer` instance.248 *249 * @param {any} base - the initial state250 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified251 * @param {Function} patchListener - optional function that will be called with all the patches produced here252 * @returns {any} a new state, or the initial state if nothing was modified253 */254 produce: IProduce255 /**256 * When true, `produce` will freeze the copies it creates.257 */258 readonly autoFreeze: boolean259 /**260 * When true, drafts are ES2015 proxies.261 */262 readonly useProxies: boolean263 /**264 * Pass true to automatically freeze all copies created by Immer.265 *266 * By default, auto-freezing is disabled in production.267 */268 setAutoFreeze(autoFreeze: boolean): void269 /**270 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is271 * always faster than using ES5 proxies.272 *273 * By default, feature detection is used, so calling this is rarely necessary.274 */275 setUseProxies(useProxies: boolean): void

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

lines 276–278why, one step back
276}
277278export interface ImmerState<T = any> {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 279–282why, one step back
279 parent?: ImmerState
280 base: T281 copy: T282 assigned: {[prop: string]: boolean; [index: number]: boolean}

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

line 283why, one step back
283}

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

lines 284–286why, one step back
284
285// Backward compatibility with --target es5286declare global {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 287–290why, one step back
287 interface Set<T> {}
288 interface Map<K, V> {}289 interface WeakSet<T> {}290 interface WeakMap<K extends object, V> {}

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

line 291why, one step back
291}