immer.d.ts
This file
Path
src/immer.d.ts
207 lines · 6.7 KiB · TypeScript
Last changed by
fix(ts): use base type as default produce() return typealeclarsonbefore adoption3aa1c4a
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): produce() return typealeclarsonbefore adoptionb52b45e
line 46why, one step back46type IsVoidLike<T> = T extends void | undefined ? 1 : 0fix(ts): return undefined from producealeclarsonbefore adoptiond60db14
line 47why, one step back47fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 48–51why, one step back48/** Converts `nothing` into `undefined` */49type FromNothing<T> = Nothing extends T ? Exclude<T, Nothing> | undefined : T5051/** The inferred return type of `produce` */fix(ts): produce() return typealeclarsonbefore adoptionb52b45e
lines 52–56why, one step back52export type Produced<T, Return> = IsVoidLike<Return> extends 053 ? FromNothing<Return>54 : IsVoidLike<Return> extends 155 ? T56 : T | FromNothing<Exclude<Return, void>>fix(ts): ensure produce returns an immutable typealeclarsonbefore adoptiondd25143
lines 57–71why, one step back5758type ImmutableTuple<T extends ReadonlyArray<any>> = {59 readonly [P in keyof T]: Immutable<T[P]>60}6162/** Convert a mutable type into a readonly type */63export type Immutable<T> = T extends object64 ? T extends AtomicObject65 ? T66 : T extends ReadonlyArray<any>67 ? Array<T[number]> extends T68 ? {[P in keyof T]: ReadonlyArray<Immutable<T[number]>>}[keyof T]69 : ImmutableTuple<T>70 : {readonly [P in keyof T]: Immutable<T[P]>}71 : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
line 72why, one step back72fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 73–92why, one step back73export interface IProduce {74 /**75 * The `produce` function takes a value and a "recipe function" (whose76 * return value often depends on the base state). The recipe function is77 * free to mutate its first argument however it wants. All mutations are78 * only ever applied to a __copy__ of the base state.79 *80 * Pass only a function to create a "curried producer" which relieves you81 * from passing the recipe function every time.82 *83 * Only plain objects and arrays are made mutable. All other objects are84 * considered uncopyable.85 *86 * Note: This function is __bound__ to its `Immer` instance.87 *88 * @param {any} base - the initial state89 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified90 * @param {Function} patchListener - optional function that will be called with all the patches produced here91 * @returns {any} a new state, or the initial state if nothing was modified92 */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 93why, one step back93 <T = any, Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 94–95why, one step back94 base: T,95 recipe: (this: D, draft: D) => Return,fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 96why, one step back96 listener?: PatchListenerfix(ts): use base type as default produce() return typealeclarsonbefore adoption3aa1c4a
line 97why, one step back97 ): Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 98why, one step back98fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 99why, one step back99 /** Curried producer with a default value */fix(ts): reorder generic parameters of IProducealeclarsonbefore adoption05157af
line 100why, one step back100 <T = any, Rest extends any[] = [], Return = void, D = Draft<T>>(fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 101–103why, one step back101 recipe: (this: D, draft: D, ...rest: Rest) => Return,102 defaultBase: T103 ): (base: Immutable<D> | undefined, ...rest: Rest) => Produced<D, Return>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
lines 104–105why, one step back104105 /** Curried producer with no default value */fix(ts): return type of produce()aleclarsonbefore adoption03ec620
lines 106–108why, one step back106 <T = any, Rest extends any[] = [], Return = void>(107 recipe: (this: Draft<T>, draft: Draft<T>, ...rest: Rest) => Return108 ): (base: Immutable<T>, ...rest: Rest) => Produced<T, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 109–113why, one step back109}110111export const produce: IProduce112export default produce113fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 114–119why, one step back114/** Use a class type for `nothing` so its type is unique */115declare class Nothing {116 // This lets us do `Exclude<T, Nothing>`117 private _: any118}119fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 120–122why, one step back120/**121 * The sentinel value returned by producers to replace the draft with undefined.122 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 123why, one step back123export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 124–207why, one step back124125/**126 * Pass true to automatically freeze all copies created by Immer.127 *128 * By default, auto-freezing is disabled in production.129 */130export function setAutoFreeze(autoFreeze: boolean): void131132/**133 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is134 * always faster than using ES5 proxies.135 *136 * By default, feature detection is used, so calling this is rarely necessary.137 */138export function setUseProxies(useProxies: boolean): void139140/**141 * Apply an array of Immer patches to the first argument.142 *143 * This function is a producer, which means copy-on-write is in effect.144 */145export function applyPatches<S>(base: S, patches: Patch[]): S146147export function original<T>(value: T): T | void148149export function isDraft(value: any): boolean150151export class Immer {152 constructor(config: {153 useProxies?: boolean154 autoFreeze?: boolean155 onAssign?: (state: ImmerState, prop: keyof any, value: any) => void156 onDelete?: (state: ImmerState, prop: keyof any) => void157 onCopy?: (state: ImmerState) => void158 })159 /**160 * The `produce` function takes a value and a "recipe function" (whose161 * return value often depends on the base state). The recipe function is162 * free to mutate its first argument however it wants. All mutations are163 * only ever applied to a __copy__ of the base state.164 *165 * Pass only a function to create a "curried producer" which relieves you166 * from passing the recipe function every time.167 *168 * Only plain objects and arrays are made mutable. All other objects are169 * considered uncopyable.170 *171 * Note: This function is __bound__ to its `Immer` instance.172 *173 * @param {any} base - the initial state174 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified175 * @param {Function} patchListener - optional function that will be called with all the patches produced here176 * @returns {any} a new state, or the initial state if nothing was modified177 */178 produce: IProduce179 /**180 * When true, `produce` will freeze the copies it creates.181 */182 readonly autoFreeze: boolean183 /**184 * When true, drafts are ES2015 proxies.185 */186 readonly useProxies: boolean187 /**188 * Pass true to automatically freeze all copies created by Immer.189 *190 * By default, auto-freezing is disabled in production.191 */192 setAutoFreeze(autoFreeze: boolean): void193 /**194 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is195 * always faster than using ES5 proxies.196 *197 * By default, feature detection is used, so calling this is rarely necessary.198 */199 setUseProxies(useProxies: boolean): void200}201202export interface ImmerState<T = any> {203 parent?: ImmerState204 base: T205 copy: T206 assigned: {[prop: string]: boolean}207}