immer.d.ts
This file
Path
src/immer.d.ts
224 lines · 7.3 KiB · TypeScript
Last changed by
fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
History of this fileTake it
fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 1–14why, one step back1/** 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 | String14fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 15–16why, one step back15type 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 back17fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 18–19why, one step back18export 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 back20>21fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 22–23why, one step back22export type DraftTuple<T extends ReadonlyArray<any>> = {23 [P in keyof T]: P extends Indices<T> ? Draft<T[P]> : neverfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 24–25why, one step back24}25fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 26–27why, one step back26export type Draft<T> = T extends never[]27 ? Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 28why, one step back28 : T extends ReadonlyArray<any>fix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
lines 29–33why, one step back29 ? T[number][] extends T30 ? DraftArray<T>31 : DraftTuple<T>32 : T extends AtomicObject33 ? Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 34why, one step back34 : T extends objectfix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
line 35why, one step back35 ? {-readonly [P in keyof T]: Draft<T[P]>}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 36–45why, one step back36 : T3738export interface Patch {39 op: "replace" | "remove" | "add"40 path: (string | number)[]41 value?: any42}4344export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void45fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
lines 46–54why, one step back46/** 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)54fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 55–58why, one step back55/** 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 back59type Produced<Base, Return> = 1 extends HasVoidLike<Return>60 ? 1 extends IsVoidLike<Return>fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
lines 61–62why, one step back61 ? Immutable<Base>62 : Immutable<Base> | FromNothing<Exclude<Return, void>>fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 63why, one step back63 : FromNothing<Return>fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
lines 64–78why, one step back6465type ImmutableTuple<T extends ReadonlyArray<any>> = {66 readonly [P in keyof T]: Immutable<T[P]>67}6869/** Convert a mutable type into a readonly type */70export type Immutable<T> = T extends object71 ? T extends AtomicObject72 ? T73 : T extends ReadonlyArray<any>74 ? Array<T[number]> extends T75 ? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]76 : ImmutableTuple<T>77 : {readonly [P in keyof T]: Immutable<T[P]>}78 : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 79why, one step back79fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 80–99why, one step back80export interface IProduce {81 /**82 * The `produce` function takes a value and a "recipe function" (whose83 * return value often depends on the base state). The recipe function is84 * free to mutate its first argument however it wants. All mutations are85 * only ever applied to a __copy__ of the base state.86 *87 * Pass only a function to create a "curried producer" which relieves you88 * from passing the recipe function every time.89 *90 * Only plain objects and arrays are made mutable. All other objects are91 * considered uncopyable.92 *93 * Note: This function is __bound__ to its `Immer` instance.94 *95 * @param {any} base - the initial state96 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified97 * @param {Function} patchListener - optional function that will be called with all the patches produced here98 * @returns {any} a new state, or the initial state if nothing was modified99 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 100–102why, one step back100 <Base, Proxy = Draft<Base>, Return = void>(101 base: Base,102 recipe: (this: Proxy, draft: Proxy) => Return,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 103why, one step back103 listener?: PatchListenerfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 104why, one step back104 ): Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 105why, one step back105fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 106why, one step back106 /** Curried producer with a default value */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 107why, one step back107 <Default = any, Base = Default, Rest extends any[] = [], Return = void>(fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 108why, one step back108 recipe: (fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 109–112why, one step back109 this: Draft<Base>,110 draft: Draft<Base>,111 ...rest: Rest112 ) => Return,fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 113why, one step back113 defaultBase: Defaultfix(ts): base type of curried produceraleclarsonbefore adoptionb357e54
lines 114–117why, one step back114 ): <T>(115 base: (Draft<T> extends Draft<Base> ? T : Base) | undefined,116 ...rest: Rest117 ) => Produced<Base, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 118–121why, one step back118119 /** Curried producer with no default value */120 <Base = any, Rest extends any[] = [], Return = void>(121 recipe: (this: Draft<Base>, draft: Draft<Base>, ...rest: Rest) => Returnfix(ts): base type of curried produceraleclarsonbefore adoptionb357e54
lines 122–125why, one step back122 ): <T>(123 base: Draft<T> extends Draft<Base> ? T : Base,124 ...rest: Rest125 ) => Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 126–130why, one step back126}127128export const produce: IProduce129export default produce130fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 131–136why, one step back131/** Use a class type for `nothing` so its type is unique */132declare class Nothing {133 // This lets us do `Exclude<T, Nothing>`134 private _: any135}136fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 137–139why, one step back137/**138 * The sentinel value returned by producers to replace the draft with undefined.139 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 140why, one step back140export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 141–224why, one step back141142/**143 * Pass true to automatically freeze all copies created by Immer.144 *145 * By default, auto-freezing is disabled in production.146 */147export function setAutoFreeze(autoFreeze: boolean): void148149/**150 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is151 * always faster than using ES5 proxies.152 *153 * By default, feature detection is used, so calling this is rarely necessary.154 */155export function setUseProxies(useProxies: boolean): void156157/**158 * Apply an array of Immer patches to the first argument.159 *160 * This function is a producer, which means copy-on-write is in effect.161 */162export function applyPatches<S>(base: S, patches: Patch[]): S163164export function original<T>(value: T): T | void165166export function isDraft(value: any): boolean167168export class Immer {169 constructor(config: {170 useProxies?: boolean171 autoFreeze?: boolean172 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void173 onDelete?: (state: ImmerState, prop: keyof any) => void174 onCopy?: (state: ImmerState) => void175 })176 /**177 * The `produce` function takes a value and a "recipe function" (whose178 * return value often depends on the base state). The recipe function is179 * free to mutate its first argument however it wants. All mutations are180 * only ever applied to a __copy__ of the base state.181 *182 * Pass only a function to create a "curried producer" which relieves you183 * from passing the recipe function every time.184 *185 * Only plain objects and arrays are made mutable. All other objects are186 * considered uncopyable.187 *188 * Note: This function is __bound__ to its `Immer` instance.189 *190 * @param {any} base - the initial state191 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified192 * @param {Function} patchListener - optional function that will be called with all the patches produced here193 * @returns {any} a new state, or the initial state if nothing was modified194 */195 produce: IProduce196 /**197 * When true, `produce` will freeze the copies it creates.198 */199 readonly autoFreeze: boolean200 /**201 * When true, drafts are ES2015 proxies.202 */203 readonly useProxies: boolean204 /**205 * Pass true to automatically freeze all copies created by Immer.206 *207 * By default, auto-freezing is disabled in production.208 */209 setAutoFreeze(autoFreeze: boolean): void210 /**211 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is212 * always faster than using ES5 proxies.213 *214 * By default, feature detection is used, so calling this is rarely necessary.215 */216 setUseProxies(useProxies: boolean): void217}218219export interface ImmerState<T = any> {220 parent?: ImmerState221 base: T222 copy: T223 assigned: {[prop: string]: boolean}224}