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 fileTake it
feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 1why, one step back1type Tail<T extends any[]> = ((...t: T) => any) extends ((style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 2–3why, one step back2 _: any,3 ...tail: infer TTfeat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 4why, one step back4) => any)style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 5–6why, one step back5 ? TT6 : []feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 7why, one step back7fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 8–9why, one step back8/** Object types that should never be mapped */9type AtomicObject =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 10–20why, one step back10 | Function11 | Map<any, any>12 | WeakMap<any, any>13 | Set<any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp18 | Boolean19 | Number20 | Stringfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 21why, one step back21refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
line 22why, one step back22export type Draft<T> = T extends AtomicObjectstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 23–26why, one step back23 ? T24 : T extends object25 ? {-readonly [K in keyof T]: Draft<T[K]>}26 : Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 27why, one step back27refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
lines 28–29why, one step back28/** Convert a mutable type into a readonly type */29export type Immutable<T> = T extends AtomicObjectstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 30–33why, one step back30 ? T31 : T extends object32 ? {readonly [K in keyof T]: Immutable<T[K]>}33 : Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 34–35why, one step back3435export interface Patch {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 36–38why, one step back36 op: "replace" | "remove" | "add"37 path: (string | number)[]38 value?: anyfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 39–42why, one step back39}4041export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void42fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 43why, one step back43/** Converts `nothing` into `undefined` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 44why, one step back44type FromNothing<T> = T extends Nothing ? undefined : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 45–46why, one step back4546/** The inferred return type of `produce` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 47why, one step back47export type Produced<Base, Return> = Return extends voidstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 48–51why, one step back48 ? Base49 : 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 back52refactor(ts): tweak IProduce typeAlec Larsonbefore adoption8925080
lines 53–71why, one step back53/**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 back72export interface IProduce {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 73–86why, one step back73 /** 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 back87style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 88–99why, one step back88 /** 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 back100style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 101–106why, one step back101 /** 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 back107}108109export const produce: IProduce110export default produce111Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda
lines 112–154why, one step back112/**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: IProduceWithPatches154fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 155–156why, one step back155/** Use a class type for `nothing` so its type is unique */156declare class Nothing {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 157–158why, one step back157 // This lets us do `Exclude<T, Nothing>`158 private _: anyfix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 159–160why, one step back159}160fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 161–163why, one step back161/**162 * The sentinel value returned by producers to replace the draft with undefined.163 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 164why, one step back164export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 165–166why, one step back165166/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 167–176why, one step back167 * To let Immer treat your class instances as plain immutable objects168 * (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 back177 * 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[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 197–212why, one step back197198/**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 back213fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 214why, one step back214/** 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 back215export function original<T>(value: T): T | void216feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 217why, one step back217/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 218why, one step back218export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 219–221why, one step back219220/** Returns true if the given value can be drafted by Immer */221export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 222–223why, one step back222223export class Immer {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 224–275why, one step back224 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): voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 276–278why, one step back276}277278export interface ImmerState<T = any> {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 279–282why, one step back279 parent?: ImmerState280 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 back283}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 284–286why, one step back284285// Backward compatibility with --target es5286declare global {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 287–290why, one step back287 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 back291}