Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

309 lines · 9.5 KiB · TypeScript

Last changed by

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

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–18why, one step back
10 | Function
11 | WeakMap<any, any>12 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String

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

line 19why, one step back
19

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 21why, one step back
21 ? T

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 22–25why, one step back
22 : T extends Map<infer K, infer V>
23 ? DraftMap<K, V>24 : T extends Set<infer V>25 ? DraftSet<V>

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

line 29why, one step back
29

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 30–35why, one step back
30// Inline these in ts 3.7
31interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3233// Inline these in ts 3.734interface DraftSet<V> extends Set<Draft<V>> {}35

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 38why, one step back
38 ? T

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 39–44why, one step back
39 : T extends Map<infer K, infer V>
40 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">41 ImmutableMap<K, V>42 : T extends Set<infer V>43 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">44 ImmutableSet<V>

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 45–47why, one step back
45 : T extends object
46 ? {readonly [K in keyof T]: Immutable<T[K]>}47 : T

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 48–51why, one step back
48
49interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}5051interface ImmutableSet<V> extends Set<Immutable<V>> {}

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

lines 52–53why, one step back
52
53export interface Patch {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

lines 57–60why, one step back
57}
5859export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void60

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 66–69why, one step back
66 ? Base
67 : Return extends Promise<infer Result>68 ? Promise<Result extends void ? Base : FromNothing<Result>>69 : FromNothing<Return>

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

line 70why, one step back
70

refactor(ts): tweak IProduce typeAlec Larsonbefore adoption8925080

lines 71–89why, one step back
71/**
72 * The `produce` function takes a value and a "recipe function" (whose73 * return value often depends on the base state). The recipe function is74 * free to mutate its first argument however it wants. All mutations are75 * only ever applied to a __copy__ of the base state.76 *77 * Pass only a function to create a "curried producer" which relieves you78 * from passing the recipe function every time.79 *80 * Only plain objects and arrays are made mutable. All other objects are81 * considered uncopyable.82 *83 * Note: This function is __bound__ to its `Immer` instance.84 *85 * @param {any} base - the initial state86 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified87 * @param {Function} patchListener - optional function that will be called with all the patches produced here88 * @returns {any} a new state, or the initial state if nothing was modified89 */

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 91–104why, one step back
91 /** Curried producer */
92 <93 Recipe extends (...args: any[]) => any,94 Params extends any[] = Parameters<Recipe>,95 T = Params[0]96 >(97 recipe: Recipe98 ): <Base extends Immutable<T>>(99 base: Base,100 ...rest: Tail<Params>101 ) => Produced<Base, ReturnType<Recipe>>102 // ^ by making the returned type generic, the actual type of the passed in object is preferred103 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe104 // 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 105why, one step back
105

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 106–117why, one step back
106 /** Curried producer with initial state */
107 <108 Recipe extends (...args: any[]) => any,109 Params extends any[] = Parameters<Recipe>,110 T = Params[0]111 >(112 recipe: Recipe,113 initialState: Immutable<T>114 ): <Base extends Immutable<T>>(115 base?: Base,116 ...rest: Tail<Params>117 ) => Produced<Base, ReturnType<Recipe>>

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

line 118why, one step back
118

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 119–124why, one step back
119 /** Normal producer */
120 <Base, D = Draft<Base>, Return = void>(121 base: Base,122 recipe: (draft: D) => Return,123 listener?: PatchListener124 ): Produced<Base, Return>

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

lines 125–129why, one step back
125}
126127export const produce: IProduce128export default produce129

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

lines 130–172why, one step back
130/**
131 * Like `produce`, but instead of just returning the new state,132 * a tuple is returned with [nextState, patches, inversePatches]133 *134 * Like produce, this function supports currying135 */136export interface IProduceWithPatches {137 /** Curried producer */138 <139 Recipe extends (...args: any[]) => any,140 Params extends any[] = Parameters<Recipe>,141 T = Params[0]142 >(143 recipe: Recipe144 ): <Base extends Immutable<T>>(145 base: Base,146 ...rest: Tail<Params>147 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]148 // ^ by making the returned type generic, the actual type of the passed in object is preferred149 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe150 // 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!151152 /** Curried producer with initial state */153 <154 Recipe extends (...args: any[]) => any,155 Params extends any[] = Parameters<Recipe>,156 T = Params[0]157 >(158 recipe: Recipe,159 initialState: Immutable<T>160 ): <Base extends Immutable<T>>(161 base?: Base,162 ...rest: Tail<Params>163 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]164165 /** Normal producer */166 <Base, D = Draft<Base>, Return = void>(167 base: Base,168 recipe: (draft: D) => Return169 ): [Produced<Base, Return>, Patch[], Patch[]]170}171export const produceWithPatches: IProduceWithPatches172

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 177–178why, one step back
177}
178

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

lines 179–181why, one step back
179/**
180 * The sentinel value returned by producers to replace the draft with undefined.181 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 183–184why, one step back
183
184/**

feat: limited class supportaleclarsonbefore adoption9877d64

lines 185–194why, one step back
185 * To let Immer treat your class instances as plain immutable objects
186 * (albeit with a custom prototype), you must define either an instance property187 * or a static property on each of your custom classes.188 *189 * Otherwise, your class instance will never be drafted, which means it won't be190 * safe to mutate in a produce callback.191 */192export const immerable: unique symbol193194/**

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

lines 195–214why, one step back
195 * Pass true to automatically freeze all copies created by Immer.
196 *197 * By default, auto-freezing is disabled in production.198 */199export function setAutoFreeze(autoFreeze: boolean): void200201/**202 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is203 * always faster than using ES5 proxies.204 *205 * By default, feature detection is used, so calling this is rarely necessary.206 */207export function setUseProxies(useProxies: boolean): void208209/**210 * Apply an array of Immer patches to the first argument.211 *212 * This function is a producer, which means copy-on-write is in effect.213 */214export function applyPatches<S>(base: S, patches: Patch[]): S

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 215–230why, one step back
215
216/**217 * Create an Immer draft from the given base state, which may be a draft itself.218 * The draft can be modified until you finalize it with the `finishDraft` function.219 */220export function createDraft<T>(base: T): Draft<T>221222/**223 * Finalize an Immer draft from a `createDraft` call, returning the base state224 * (if no changes were made) or a modified copy. The draft must *not* be225 * mutated afterwards.226 *227 * Pass a function as the 2nd argument to generate Immer patches based on the228 * changes that were made.229 */230export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>

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

line 231why, one step back
231

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

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

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

feat: export isDraftablealeclarsonbefore adoptionc89d47d

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

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

lines 240–241why, one step back
240
241export class Immer {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 242–293why, one step back
242 constructor(config: {
243 useProxies?: boolean244 autoFreeze?: boolean245 onAssign?: (246 state: ImmerState,247 prop: string | number,248 value: unknown249 ) => void250 onDelete?: (state: ImmerState, prop: string | number) => void251 onCopy?: (state: ImmerState) => void252 })253 /**254 * The `produce` function takes a value and a "recipe function" (whose255 * return value often depends on the base state). The recipe function is256 * free to mutate its first argument however it wants. All mutations are257 * only ever applied to a __copy__ of the base state.258 *259 * Pass only a function to create a "curried producer" which relieves you260 * from passing the recipe function every time.261 *262 * Only plain objects and arrays are made mutable. All other objects are263 * considered uncopyable.264 *265 * Note: This function is __bound__ to its `Immer` instance.266 *267 * @param {any} base - the initial state268 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified269 * @param {Function} patchListener - optional function that will be called with all the patches produced here270 * @returns {any} a new state, or the initial state if nothing was modified271 */272 produce: IProduce273 /**274 * When true, `produce` will freeze the copies it creates.275 */276 readonly autoFreeze: boolean277 /**278 * When true, drafts are ES2015 proxies.279 */280 readonly useProxies: boolean281 /**282 * Pass true to automatically freeze all copies created by Immer.283 *284 * By default, auto-freezing is disabled in production.285 */286 setAutoFreeze(autoFreeze: boolean): void287 /**288 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is289 * always faster than using ES5 proxies.290 *291 * By default, feature detection is used, so calling this is rarely necessary.292 */293 setUseProxies(useProxies: boolean): void

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

lines 294–296why, one step back
294}
295296export interface ImmerState<T = any> {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 297–300why, one step back
297 parent?: ImmerState
298 base: T299 copy: T300 assigned: {[prop: string]: boolean; [index: number]: boolean}

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

line 301why, one step back
301}

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

lines 302–304why, one step back
302
303// Backward compatibility with --target es5304declare global {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 305–308why, one step back
305 interface Set<T> {}
306 interface Map<K, V> {}307 interface WeakSet<T> {}308 interface WeakMap<K extends object, V> {}

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

line 309why, one step back
309}