immer.d.ts
This file
Path
src/immer.d.ts
245 lines · 7.7 KiB · TypeScript
Last changed by
feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
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–83why, 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 ): (81 state: Immutable<T>,82 ...rest: Tail<Params>83 ) => Produced<Immutable<T>, ReturnType<Recipe>>fix(ts): parameter types of curried producersaleclarsonbefore adoption89f6443
line 84why, one step back84feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 85–103why, one step back85 /** Curried producer with initial state */86 <87 Recipe extends (...args: any[]) => any,88 Params extends any[] = Parameters<Recipe>,89 T = Params[0]90 >(91 recipe: Recipe,92 initialState: T93 ): (94 state?: Immutable<T>,95 ...rest: Tail<Params>96 ) => Produced<Immutable<T>, ReturnType<Recipe>>9798 /** Normal producer */99 <Base, D = Draft<Base>, Return = void>(100 base: Base,101 recipe: (this: D, draft: D) => Return,102 listener?: PatchListener103 ): Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 104–108why, one step back104}105106export const produce: IProduce107export default produce108fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 109–114why, one step back109/** Use a class type for `nothing` so its type is unique */110declare class Nothing {111 // This lets us do `Exclude<T, Nothing>`112 private _: any113}114fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 115–117why, one step back115/**116 * The sentinel value returned by producers to replace the draft with undefined.117 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 118why, one step back118export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 119–120why, one step back119120/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 121–130why, one step back121 * To let Immer treat your class instances as plain immutable objects122 * (albeit with a custom prototype), you must define either an instance property123 * or a static property on each of your custom classes.124 *125 * Otherwise, your class instance will never be drafted, which means it won't be126 * safe to mutate in a produce callback.127 */128export const immerable: unique symbol129130/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 131–150why, one step back131 * Pass true to automatically freeze all copies created by Immer.132 *133 * By default, auto-freezing is disabled in production.134 */135export function setAutoFreeze(autoFreeze: boolean): void136137/**138 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is139 * always faster than using ES5 proxies.140 *141 * By default, feature detection is used, so calling this is rarely necessary.142 */143export function setUseProxies(useProxies: boolean): void144145/**146 * Apply an array of Immer patches to the first argument.147 *148 * This function is a producer, which means copy-on-write is in effect.149 */150export function applyPatches<S>(base: S, patches: Patch[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 151–166why, one step back151152/**153 * Create an Immer draft from the given base state, which may be a draft itself.154 * The draft can be modified until you finalize it with the `finishDraft` function.155 */156export function createDraft<T>(base: T): Draft<T>157158/**159 * Finalize an Immer draft from a `createDraft` call, returning the base state160 * (if no changes were made) or a modified copy. The draft must *not* be161 * mutated afterwards.162 *163 * Pass a function as the 2nd argument to generate Immer patches based on the164 * changes that were made.165 */166export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 167why, one step back167fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 168why, one step back168/** Get the underlying object that is represented by the given draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 169–170why, one step back169export function original<T>(value: T): T | void170feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 171why, one step back171/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 172why, one step back172export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 173–175why, one step back173174/** Returns true if the given value can be drafted by Immer */175export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 176–180why, one step back176177export class Immer {178 constructor(config: {179 useProxies?: boolean180 autoFreeze?: booleanfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
lines 181–186why, one step back181 onAssign?: (182 state: ImmerState,183 prop: string | number,184 value: unknown185 ) => void186 onDelete?: (state: ImmerState, prop: string | number) => voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 187–235why, one step back187 onCopy?: (state: ImmerState) => void188 })189 /**190 * The `produce` function takes a value and a "recipe function" (whose191 * return value often depends on the base state). The recipe function is192 * free to mutate its first argument however it wants. All mutations are193 * only ever applied to a __copy__ of the base state.194 *195 * Pass only a function to create a "curried producer" which relieves you196 * from passing the recipe function every time.197 *198 * Only plain objects and arrays are made mutable. All other objects are199 * considered uncopyable.200 *201 * Note: This function is __bound__ to its `Immer` instance.202 *203 * @param {any} base - the initial state204 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified205 * @param {Function} patchListener - optional function that will be called with all the patches produced here206 * @returns {any} a new state, or the initial state if nothing was modified207 */208 produce: IProduce209 /**210 * When true, `produce` will freeze the copies it creates.211 */212 readonly autoFreeze: boolean213 /**214 * When true, drafts are ES2015 proxies.215 */216 readonly useProxies: boolean217 /**218 * Pass true to automatically freeze all copies created by Immer.219 *220 * By default, auto-freezing is disabled in production.221 */222 setAutoFreeze(autoFreeze: boolean): void223 /**224 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is225 * always faster than using ES5 proxies.226 *227 * By default, feature detection is used, so calling this is rarely necessary.228 */229 setUseProxies(useProxies: boolean): void230}231232export interface ImmerState<T = any> {233 parent?: ImmerState234 base: T235 copy: Tfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 236why, one step back236 assigned: {[prop: string]: boolean; [index: number]: boolean}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 237why, one step back237}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 238–245why, one step back238239// Backward compatibility with --target es5240declare global {241 interface Set<T> {}242 interface Map<K, V> {}243 interface WeakSet<T> {}244 interface WeakMap<K extends object, V> {}245}