Skip to content
Myr

immer.d.ts

This file

Path

src/immer.d.ts

209 lines · 6.8 KiB · TypeScript

Last changed by

fix(ts): base type of curried produceraleclarsonbefore adoptionb357e54

History of this file

Take it

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

lines 1–14why, one step back
1/** Object types that should never be mapped */
2type AtomicObject =3 | Function4 | Map<any, any>5 | WeakMap<any, any>6 | Set<any>7 | WeakSet<any>8 | Promise<any>9 | Date10 | RegExp11 | Boolean12 | Number13 | String14

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

lines 15–16why, one step back
15type ArrayMethod = Exclude<keyof [], number>
16type Indices<T> = Exclude<keyof T, ArrayMethod>

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

line 17why, one step back
17

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

lines 18–19why, one step back
18export type DraftArray<T extends ReadonlyArray<any>> = Array<
19 {[P in Indices<T>]: Draft<T[P]>}[Indices<T>]

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

lines 20–21why, one step back
20>
21

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

lines 22–23why, one step back
22export type DraftTuple<T extends ReadonlyArray<any>> = {
23 [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : never

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

lines 24–25why, one step back
24}
25

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

lines 26–27why, one step back
26export type Draft<T> = T extends never[]
27 ? T

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

line 28why, one step back
28 : T extends ReadonlyArray<any>

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

lines 29–33why, one step back
29 ? T[number][] extends T
30 ? DraftArray<T>31 : DraftTuple<T>32 : T extends AtomicObject33 ? T

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

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

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

line 35why, one step back
35 ? {-readonly [P in keyof T]: Draft<T[P]>}

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

lines 36–45why, one step back
36 : T
3738export interface Patch {39 op: "replace" | "remove" | "add"40 path: (string | number)[]41 value?: any42}4344export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void45

fix(ts): return undefined from producealeclarsonbefore adoptiond60db14

lines 46–54why, one step back
46/** Includes 1 when `void` or `undefined` exists in type `T` */
47type HasVoidLike<T> = (void extends T ? 1 : 0) | (undefined extends T ? 1 : 0)4849/** Includes 1 when type `T` is `void` or `undefined` (or both) */50type IsVoidLike<T> =51 | (T extends void ? 1 : 0)52 | (T extends undefined ? 1 : 0)53 | (T extends void | undefined ? 1 : 0)54

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 55–58why, one step back
55/** Converts `nothing` into `undefined` */
56type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T5758/** The inferred return type of `produce` */

fix(ts): return undefined from producealeclarsonbefore adoptiond60db14

lines 59–60why, one step back
59type Produced<Base, Return> = 1 extends HasVoidLike<Return>
60 ? 1 extends IsVoidLike<Return>

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 61–64why, one step back
61 ? Base
62 : Base | FromNothing<Exclude<Return, void>>63 : FromNothing<Return>64

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

lines 65–84why, one step back
65export interface IProduce {
66 /**67 * The `produce` function takes a value and a "recipe function" (whose68 * return value often depends on the base state). The recipe function is69 * free to mutate its first argument however it wants. All mutations are70 * only ever applied to a __copy__ of the base state.71 *72 * Pass only a function to create a "curried producer" which relieves you73 * from passing the recipe function every time.74 *75 * Only plain objects and arrays are made mutable. All other objects are76 * considered uncopyable.77 *78 * Note: This function is __bound__ to its `Immer` instance.79 *80 * @param {any} base - the initial state81 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified82 * @param {Function} patchListener - optional function that will be called with all the patches produced here83 * @returns {any} a new state, or the initial state if nothing was modified84 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 85–87why, one step back
85 <Base, Proxy = Draft<Base>, Return = void>(
86 base: Base,87 recipe: (this: Proxy, draft: Proxy) => Return,

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

line 88why, one step back
88 listener?: PatchListener

fix(ts): improve IProducealeclarsonbefore adoption8666d32

line 89why, one step back
89 ): Produced<Base, Return>

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

line 90why, one step back
90

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

line 91why, one step back
91 /** Curried producer with a default value */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

line 92why, one step back
92 <Default = any, Base = Default, Rest extends any[] = [], Return = void>(

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

line 93why, one step back
93 recipe: (

fix(ts): improve IProducealeclarsonbefore adoption8666d32

lines 94–97why, one step back
94 this: Draft<Base>,
95 draft: Draft<Base>,96 ...rest: Rest97 ) => Return,

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

line 98why, one step back
98 defaultBase: Default

fix(ts): base type of curried produceraleclarsonbefore adoptionb357e54

lines 99–102why, one step back
99 ): <T>(
100 base: (Draft<T> extends Draft<Base> ? T : Base) | undefined,101 ...rest: Rest102 ) => Produced<Base, Return>

fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443

lines 103–106why, one step back
103
104 /** Curried producer with no default value */105 <Base = any, Rest extends any[] = [], Return = void>(106 recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Return

fix(ts): base type of curried produceraleclarsonbefore adoptionb357e54

lines 107–110why, one step back
107 ): <T>(
108 base: Draft<T> extends Draft<Base> ? T : Base,109 ...rest: Rest110 ) => Produced<Base, Return>

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

lines 111–115why, one step back
111}
112113export const produce: IProduce114export default produce115

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 122–124why, one step back
122/**
123 * The sentinel value returned by producers to replace the draft with undefined.124 */

fix(ts): improve IProducealeclarsonbefore adoption8666d32

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

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

lines 126–209why, one step back
126
127/**128 * Pass true to automatically freeze all copies created by Immer.129 *130 * By default, auto-freezing is disabled in production.131 */132export function setAutoFreeze(autoFreeze: boolean): void133134/**135 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is136 * always faster than using ES5 proxies.137 *138 * By default, feature detection is used, so calling this is rarely necessary.139 */140export function setUseProxies(useProxies: boolean): void141142/**143 * Apply an array of Immer patches to the first argument.144 *145 * This function is a producer, which means copy-on-write is in effect.146 */147export function applyPatches<S>(base: S, patches: Patch[]): S148149export function original<T>(value: T): T | void150151export function isDraft(value: any): boolean152153export class Immer {154 constructor(config: {155 useProxies?: boolean156 autoFreeze?: boolean157 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void158 onDelete?: (state: ImmerState, prop: keyof any) => void159 onCopy?: (state: ImmerState) => void160 })161 /**162 * The `produce` function takes a value and a "recipe function" (whose163 * return value often depends on the base state). The recipe function is164 * free to mutate its first argument however it wants. All mutations are165 * only ever applied to a __copy__ of the base state.166 *167 * Pass only a function to create a "curried producer" which relieves you168 * from passing the recipe function every time.169 *170 * Only plain objects and arrays are made mutable. All other objects are171 * considered uncopyable.172 *173 * Note: This function is __bound__ to its `Immer` instance.174 *175 * @param {any} base - the initial state176 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified177 * @param {Function} patchListener - optional function that will be called with all the patches produced here178 * @returns {any} a new state, or the initial state if nothing was modified179 */180 produce: IProduce181 /**182 * When true, `produce` will freeze the copies it creates.183 */184 readonly autoFreeze: boolean185 /**186 * When true, drafts are ES2015 proxies.187 */188 readonly useProxies: boolean189 /**190 * Pass true to automatically freeze all copies created by Immer.191 *192 * By default, auto-freezing is disabled in production.193 */194 setAutoFreeze(autoFreeze: boolean): void195 /**196 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is197 * always faster than using ES5 proxies.198 *199 * By default, feature detection is used, so calling this is rarely necessary.200 */201 setUseProxies(useProxies: boolean): void202}203204export interface ImmerState<T = any> {205 parent?: ImmerState206 base: T207 copy: T208 assigned: {[prop: string]: boolean}209}