immer.d.ts
This file
Path
src/immer.d.ts
248 lines · 8.0 KiB · TypeScript
Last changed by
feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
History of this fileTake it
feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 1–7why, one step back1type Tail<T extends any[]> = ((...t: T) => any) extends ((2 _: any,3 ...tail: infer TT4) => any)5 ? TT6 : []7fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 8–21why, one step back8/** Object types that should never be mapped */9type AtomicObject =10 | Function11 | Map<any, any>12 | WeakMap<any, any>13 | Set<any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp18 | Boolean19 | Number20 | String21refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
lines 22–26why, one step back22export type Draft<T> = T extends AtomicObject23 ? T24 : T extends object25 ? {-readonly [K in keyof T]: Draft<T[K]>}26 : T // mostly: unknown & anyfix: 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 AtomicObjectfix(ts): improve Draft<T> with genericsaleclarsonbefore adoptioncc08cb7
line 30why, one step back30 ? Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 31why, one step back31 : T extends objectrefactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
line 32why, one step back32 ? {readonly [K in keyof T]: Immutable<T[K]>}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 33–42why, one step back33 : T3435export interface Patch {36 op: "replace" | "remove" | "add"37 path: (string | number)[]38 value?: any39}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
lines 47–51why, one step back47export type Produced<Base, Return> = Return extends void48 ? Base49 : Return extends Promise<infer Result>50 ? Promise<Result extends void ? Base : FromNothing<Result>>51 : FromNothing<Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 52–72why, one step back52export interface IProduce {53 /**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 */72feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 73–79why, 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: Recipefeat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 80–81why, one step back80 ): <S extends Immutable<T>>(81 state: S,feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 82why, one step back82 ...rest: Tail<Params>feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 83–86why, one step back83 ) => Produced<S, 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 back87feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 88–94why, 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,feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 95–97why, one step back95 initialState: Immutable<T>96 ): <S extends Immutable<T>>(97 state?: S,feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 98why, one step back98 ...rest: Tail<Params>feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
line 99why, one step back99 ) => Produced<S, ReturnType<Recipe>>feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 100–106why, one step back100101 /** Normal producer */102 <Base, D = Draft<Base>, Return = void>(103 base: Base,104 recipe: (this: D, 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 produce111fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 112–117why, one step back112/** Use a class type for `nothing` so its type is unique */113declare class Nothing {114 // This lets us do `Exclude<T, Nothing>`115 private _: any116}117fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 118–120why, one step back118/**119 * The sentinel value returned by producers to replace the draft with undefined.120 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 121why, one step back121export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 122–123why, one step back122123/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 124–133why, one step back124 * To let Immer treat your class instances as plain immutable objects125 * (albeit with a custom prototype), you must define either an instance property126 * or a static property on each of your custom classes.127 *128 * Otherwise, your class instance will never be drafted, which means it won't be129 * safe to mutate in a produce callback.130 */131export const immerable: unique symbol132133/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 134–153why, one step back134 * Pass true to automatically freeze all copies created by Immer.135 *136 * By default, auto-freezing is disabled in production.137 */138export function setAutoFreeze(autoFreeze: boolean): void139140/**141 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is142 * always faster than using ES5 proxies.143 *144 * By default, feature detection is used, so calling this is rarely necessary.145 */146export function setUseProxies(useProxies: boolean): void147148/**149 * Apply an array of Immer patches to the first argument.150 *151 * This function is a producer, which means copy-on-write is in effect.152 */153export function applyPatches<S>(base: S, patches: Patch[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 154–169why, one step back154155/**156 * Create an Immer draft from the given base state, which may be a draft itself.157 * The draft can be modified until you finalize it with the `finishDraft` function.158 */159export function createDraft<T>(base: T): Draft<T>160161/**162 * Finalize an Immer draft from a `createDraft` call, returning the base state163 * (if no changes were made) or a modified copy. The draft must *not* be164 * mutated afterwards.165 *166 * Pass a function as the 2nd argument to generate Immer patches based on the167 * changes that were made.168 */169export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 170why, one step back170fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 171why, one step back171/** Get the underlying object that is represented by the given draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 172–173why, one step back172export function original<T>(value: T): T | void173feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 174why, one step back174/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 175why, one step back175export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 176–178why, one step back176177/** Returns true if the given value can be drafted by Immer */178export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 179–183why, one step back179180export class Immer {181 constructor(config: {182 useProxies?: boolean183 autoFreeze?: booleanfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
lines 184–189why, one step back184 onAssign?: (185 state: ImmerState,186 prop: string | number,187 value: unknown188 ) => void189 onDelete?: (state: ImmerState, prop: string | number) => voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 190–238why, one step back190 onCopy?: (state: ImmerState) => void191 })192 /**193 * The `produce` function takes a value and a "recipe function" (whose194 * return value often depends on the base state). The recipe function is195 * free to mutate its first argument however it wants. All mutations are196 * only ever applied to a __copy__ of the base state.197 *198 * Pass only a function to create a "curried producer" which relieves you199 * from passing the recipe function every time.200 *201 * Only plain objects and arrays are made mutable. All other objects are202 * considered uncopyable.203 *204 * Note: This function is __bound__ to its `Immer` instance.205 *206 * @param {any} base - the initial state207 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified208 * @param {Function} patchListener - optional function that will be called with all the patches produced here209 * @returns {any} a new state, or the initial state if nothing was modified210 */211 produce: IProduce212 /**213 * When true, `produce` will freeze the copies it creates.214 */215 readonly autoFreeze: boolean216 /**217 * When true, drafts are ES2015 proxies.218 */219 readonly useProxies: boolean220 /**221 * Pass true to automatically freeze all copies created by Immer.222 *223 * By default, auto-freezing is disabled in production.224 */225 setAutoFreeze(autoFreeze: boolean): void226 /**227 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is228 * always faster than using ES5 proxies.229 *230 * By default, feature detection is used, so calling this is rarely necessary.231 */232 setUseProxies(useProxies: boolean): void233}234235export interface ImmerState<T = any> {236 parent?: ImmerState237 base: T238 copy: Tfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 239why, one step back239 assigned: {[prop: string]: boolean; [index: number]: boolean}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 240why, one step back240}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 241–248why, one step back241242// Backward compatibility with --target es5243declare global {244 interface Set<T> {}245 interface Map<K, V> {}246 interface WeakSet<T> {}247 interface WeakMap<K extends object, V> {}248}