immer.d.ts
This file
Path
src/immer.d.ts
249 lines · 8.0 KiB · TypeScript
Last changed by
chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae
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>chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae
line 52why, one step back52fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 53–73why, one step back53export interface IProduce {54 /**55 * The `produce` function takes a value and a "recipe function" (whose56 * return value often depends on the base state). The recipe function is57 * free to mutate its first argument however it wants. All mutations are58 * only ever applied to a __copy__ of the base state.59 *60 * Pass only a function to create a "curried producer" which relieves you61 * from passing the recipe function every time.62 *63 * Only plain objects and arrays are made mutable. All other objects are64 * considered uncopyable.65 *66 * Note: This function is __bound__ to its `Immer` instance.67 *68 * @param {any} base - the initial state69 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified70 * @param {Function} patchListener - optional function that will be called with all the patches produced here71 * @returns {any} a new state, or the initial state if nothing was modified72 */73feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 74–80why, one step back74 /** Curried producer */75 <76 Recipe extends (...args: any[]) => any,77 Params extends any[] = Parameters<Recipe>,78 T = Params[0]79 >(80 recipe: Recipefeat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 81–82why, one step back81 ): <S extends Immutable<T>>(82 state: S,feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 83why, one step back83 ...rest: Tail<Params>feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 84–87why, one step back84 ) => Produced<S, ReturnType<Recipe>>85 // ^ by making the returned type generic, the actual type of the passed in object is preferred86 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe87 // 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 88why, one step back88feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 89–95why, one step back89 /** Curried producer with initial state */90 <91 Recipe extends (...args: any[]) => any,92 Params extends any[] = Parameters<Recipe>,93 T = Params[0]94 >(95 recipe: Recipe,feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
lines 96–98why, one step back96 initialState: Immutable<T>97 ): <S extends Immutable<T>>(98 state?: S,feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 99why, one step back99 ...rest: Tail<Params>feat(ts): improve curried producersMichel Weststratebefore adoption3cb0fb3
line 100why, one step back100 ) => Produced<S, ReturnType<Recipe>>feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 101–104why, one step back101102 /** Normal producer */103 <Base, D = Draft<Base>, Return = void>(104 base: Base,chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae
line 105why, one step back105 recipe: (draft: D) => Return,feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
lines 106–107why, one step back106 listener?: PatchListener107 ): Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 108–112why, one step back108}109110export const produce: IProduce111export default produce112fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 113–118why, one step back113/** Use a class type for `nothing` so its type is unique */114declare class Nothing {115 // This lets us do `Exclude<T, Nothing>`116 private _: any117}118fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 119–121why, one step back119/**120 * The sentinel value returned by producers to replace the draft with undefined.121 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 122why, one step back122export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 123–124why, one step back123124/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 125–134why, one step back125 * To let Immer treat your class instances as plain immutable objects126 * (albeit with a custom prototype), you must define either an instance property127 * or a static property on each of your custom classes.128 *129 * Otherwise, your class instance will never be drafted, which means it won't be130 * safe to mutate in a produce callback.131 */132export const immerable: unique symbol133134/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 135–154why, one step back135 * Pass true to automatically freeze all copies created by Immer.136 *137 * By default, auto-freezing is disabled in production.138 */139export function setAutoFreeze(autoFreeze: boolean): void140141/**142 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is143 * always faster than using ES5 proxies.144 *145 * By default, feature detection is used, so calling this is rarely necessary.146 */147export function setUseProxies(useProxies: boolean): void148149/**150 * Apply an array of Immer patches to the first argument.151 *152 * This function is a producer, which means copy-on-write is in effect.153 */154export function applyPatches<S>(base: S, patches: Patch[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 155–170why, one step back155156/**157 * Create an Immer draft from the given base state, which may be a draft itself.158 * The draft can be modified until you finalize it with the `finishDraft` function.159 */160export function createDraft<T>(base: T): Draft<T>161162/**163 * Finalize an Immer draft from a `createDraft` call, returning the base state164 * (if no changes were made) or a modified copy. The draft must *not* be165 * mutated afterwards.166 *167 * Pass a function as the 2nd argument to generate Immer patches based on the168 * changes that were made.169 */170export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 171why, one step back171fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 172why, one step back172/** Get the underlying object that is represented by the given draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 173–174why, one step back173export function original<T>(value: T): T | void174feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 175why, one step back175/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 176why, one step back176export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 177–179why, one step back177178/** Returns true if the given value can be drafted by Immer */179export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 180–184why, one step back180181export class Immer {182 constructor(config: {183 useProxies?: boolean184 autoFreeze?: booleanfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
lines 185–190why, one step back185 onAssign?: (186 state: ImmerState,187 prop: string | number,188 value: unknown189 ) => void190 onDelete?: (state: ImmerState, prop: string | number) => voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 191–239why, one step back191 onCopy?: (state: ImmerState) => void192 })193 /**194 * The `produce` function takes a value and a "recipe function" (whose195 * return value often depends on the base state). The recipe function is196 * free to mutate its first argument however it wants. All mutations are197 * only ever applied to a __copy__ of the base state.198 *199 * Pass only a function to create a "curried producer" which relieves you200 * from passing the recipe function every time.201 *202 * Only plain objects and arrays are made mutable. All other objects are203 * considered uncopyable.204 *205 * Note: This function is __bound__ to its `Immer` instance.206 *207 * @param {any} base - the initial state208 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified209 * @param {Function} patchListener - optional function that will be called with all the patches produced here210 * @returns {any} a new state, or the initial state if nothing was modified211 */212 produce: IProduce213 /**214 * When true, `produce` will freeze the copies it creates.215 */216 readonly autoFreeze: boolean217 /**218 * When true, drafts are ES2015 proxies.219 */220 readonly useProxies: boolean221 /**222 * Pass true to automatically freeze all copies created by Immer.223 *224 * By default, auto-freezing is disabled in production.225 */226 setAutoFreeze(autoFreeze: boolean): void227 /**228 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is229 * always faster than using ES5 proxies.230 *231 * By default, feature detection is used, so calling this is rarely necessary.232 */233 setUseProxies(useProxies: boolean): void234}235236export interface ImmerState<T = any> {237 parent?: ImmerState238 base: T239 copy: Tfix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 240why, one step back240 assigned: {[prop: string]: boolean; [index: number]: boolean}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 241why, one step back241}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 242–249why, one step back242243// Backward compatibility with --target es5244declare global {245 interface Set<T> {}246 interface Map<K, V> {}247 interface WeakSet<T> {}248 interface WeakMap<K extends object, V> {}249}