immer.d.ts
This file
Path
src/immer.d.ts
309 lines · 9.5 KiB · TypeScript
Last changed by
feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
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–18why, one step back10 | Function11 | WeakMap<any, any>12 | WeakSet<any>13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | Stringfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 19why, one step back19refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
line 20why, one step back20export type Draft<T> = T extends AtomicObjectstyle: use tabsuse-tabsbefore adoptiona9c0fd0
line 21why, one step back21 ? Tfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 22–25why, one step back22 : T extends Map<infer K, infer V>23 ? DraftMap<K, V>24 : T extends Set<infer V>25 ? DraftSet<V>style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 26–28why, one step back26 : T extends object27 ? {-readonly [K in keyof T]: Draft<T[K]>}28 : Tfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 29why, one step back29feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 30–35why, one step back30// Inline these in ts 3.731interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3233// Inline these in ts 3.734interface DraftSet<V> extends Set<Draft<V>> {}35refactor(ts): Simpler draft / immutable implementationMichel Weststratebefore adoption7e971b6
lines 36–37why, one step back36/** Convert a mutable type into a readonly type */37export type Immutable<T> = T extends AtomicObjectstyle: use tabsuse-tabsbefore adoptiona9c0fd0
line 38why, one step back38 ? Tfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 39–44why, one step back39 : T extends Map<infer K, infer V>40 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">41 ImmutableMap<K, V>42 : T extends Set<infer V>43 ? // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">44 ImmutableSet<V>style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 45–47why, one step back45 : T extends object46 ? {readonly [K in keyof T]: Immutable<T[K]>}47 : Tfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 48–51why, one step back4849interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}5051interface ImmutableSet<V> extends Set<Immutable<V>> {}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 52–53why, one step back5253export interface Patch {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 54–56why, one step back54 op: "replace" | "remove" | "add"55 path: (string | number)[]56 value?: anyfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 57–60why, one step back57}5859export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void60fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 61why, one step back61/** Converts `nothing` into `undefined` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 62why, one step back62type FromNothing<T> = T extends Nothing ? undefined : Tfix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 63–64why, one step back6364/** The inferred return type of `produce` */Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 65why, one step back65export type Produced<Base, Return> = Return extends voidstyle: use tabsuse-tabsbefore adoptiona9c0fd0
lines 66–69why, one step back66 ? Base67 : Return extends Promise<infer Result>68 ? Promise<Result extends void ? Base : FromNothing<Result>>69 : FromNothing<Return>chore(ts): Clean up `this` from typingsMichel Weststratebefore adoptiondfb66ae
line 70why, one step back70refactor(ts): tweak IProduce typeAlec Larsonbefore adoption8925080
lines 71–89why, one step back71/**72 * The `produce` function takes a value and a "recipe function" (whose73 * return value often depends on the base state). The recipe function is74 * free to mutate its first argument however it wants. All mutations are75 * only ever applied to a __copy__ of the base state.76 *77 * Pass only a function to create a "curried producer" which relieves you78 * from passing the recipe function every time.79 *80 * Only plain objects and arrays are made mutable. All other objects are81 * considered uncopyable.82 *83 * Note: This function is __bound__ to its `Immer` instance.84 *85 * @param {any} base - the initial state86 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified87 * @param {Function} patchListener - optional function that will be called with all the patches produced here88 * @returns {any} a new state, or the initial state if nothing was modified89 */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 90why, one step back90export interface IProduce {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 91–104why, one step back91 /** Curried producer */92 <93 Recipe extends (...args: any[]) => any,94 Params extends any[] = Parameters<Recipe>,95 T = Params[0]96 >(97 recipe: Recipe98 ): <Base extends Immutable<T>>(99 base: Base,100 ...rest: Tail<Params>101 ) => Produced<Base, ReturnType<Recipe>>102 // ^ by making the returned type generic, the actual type of the passed in object is preferred103 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe104 // 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 105why, one step back105style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 106–117why, one step back106 /** Curried producer with initial state */107 <108 Recipe extends (...args: any[]) => any,109 Params extends any[] = Parameters<Recipe>,110 T = Params[0]111 >(112 recipe: Recipe,113 initialState: Immutable<T>114 ): <Base extends Immutable<T>>(115 base?: Base,116 ...rest: Tail<Params>117 ) => Produced<Base, ReturnType<Recipe>>feat(ts): Improve type inference based on the first arg of the recipe functionMichel Weststratebefore adoptionacf8a57
line 118why, one step back118style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 119–124why, one step back119 /** Normal producer */120 <Base, D = Draft<Base>, Return = void>(121 base: Base,122 recipe: (draft: D) => Return,123 listener?: PatchListener124 ): Produced<Base, Return>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 125–129why, one step back125}126127export const produce: IProduce128export default produce129Merge pull request #402 from immerjs/produce-with-patchesMichel Weststratebefore adoptiona16deda
lines 130–172why, one step back130/**131 * Like `produce`, but instead of just returning the new state,132 * a tuple is returned with [nextState, patches, inversePatches]133 *134 * Like produce, this function supports currying135 */136export interface IProduceWithPatches {137 /** Curried producer */138 <139 Recipe extends (...args: any[]) => any,140 Params extends any[] = Parameters<Recipe>,141 T = Params[0]142 >(143 recipe: Recipe144 ): <Base extends Immutable<T>>(145 base: Base,146 ...rest: Tail<Params>147 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]148 // ^ by making the returned type generic, the actual type of the passed in object is preferred149 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe150 // 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!151152 /** Curried producer with initial state */153 <154 Recipe extends (...args: any[]) => any,155 Params extends any[] = Parameters<Recipe>,156 T = Params[0]157 >(158 recipe: Recipe,159 initialState: Immutable<T>160 ): <Base extends Immutable<T>>(161 base?: Base,162 ...rest: Tail<Params>163 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]164165 /** Normal producer */166 <Base, D = Draft<Base>, Return = void>(167 base: Base,168 recipe: (draft: D) => Return169 ): [Produced<Base, Return>, Patch[], Patch[]]170}171export const produceWithPatches: IProduceWithPatches172fix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 173–174why, one step back173/** Use a class type for `nothing` so its type is unique */174declare class Nothing {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 175–176why, one step back175 // This lets us do `Exclude<T, Nothing>`176 private _: anyfix(ts): improve IProducealeclarsonbefore adoption8666d32
lines 177–178why, one step back177}178fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 179–181why, one step back179/**180 * The sentinel value returned by producers to replace the draft with undefined.181 */fix(ts): improve IProducealeclarsonbefore adoption8666d32
line 182why, one step back182export const nothing: Nothingfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 183–184why, one step back183184/**feat: limited class supportaleclarsonbefore adoption9877d64
lines 185–194why, one step back185 * To let Immer treat your class instances as plain immutable objects186 * (albeit with a custom prototype), you must define either an instance property187 * or a static property on each of your custom classes.188 *189 * Otherwise, your class instance will never be drafted, which means it won't be190 * safe to mutate in a produce callback.191 */192export const immerable: unique symbol193194/**fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 195–214why, one step back195 * Pass true to automatically freeze all copies created by Immer.196 *197 * By default, auto-freezing is disabled in production.198 */199export function setAutoFreeze(autoFreeze: boolean): void200201/**202 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is203 * always faster than using ES5 proxies.204 *205 * By default, feature detection is used, so calling this is rarely necessary.206 */207export function setUseProxies(useProxies: boolean): void208209/**210 * Apply an array of Immer patches to the first argument.211 *212 * This function is a producer, which means copy-on-write is in effect.213 */214export function applyPatches<S>(base: S, patches: Patch[]): SMerge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 215–230why, one step back215216/**217 * Create an Immer draft from the given base state, which may be a draft itself.218 * The draft can be modified until you finalize it with the `finishDraft` function.219 */220export function createDraft<T>(base: T): Draft<T>221222/**223 * Finalize an Immer draft from a `createDraft` call, returning the base state224 * (if no changes were made) or a modified copy. The draft must *not* be225 * mutated afterwards.226 *227 * Pass a function as the 2nd argument to generate Immer patches based on the228 * changes that were made.229 */230export function finishDraft<T>(draft: T, listener?: PatchListener): Immutable<T>fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 231why, one step back231fix(ts): Immer class options and ImmerState typesaleclarsonbefore adoption5ff645c
line 232why, one step back232/** Get the underlying object that is represented by the given draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 233–234why, one step back233export function original<T>(value: T): T | void234feat: export isDraftablealeclarsonbefore adoptionc89d47d
line 235why, one step back235/** Returns true if the given value is an Immer draft */fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 236why, one step back236export function isDraft(value: any): booleanfeat: export isDraftablealeclarsonbefore adoptionc89d47d
lines 237–239why, one step back237238/** Returns true if the given value can be drafted by Immer */239export function isDraftable(value: any): booleanfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 240–241why, one step back240241export class Immer {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 242–293why, one step back242 constructor(config: {243 useProxies?: boolean244 autoFreeze?: boolean245 onAssign?: (246 state: ImmerState,247 prop: string | number,248 value: unknown249 ) => void250 onDelete?: (state: ImmerState, prop: string | number) => void251 onCopy?: (state: ImmerState) => void252 })253 /**254 * The `produce` function takes a value and a "recipe function" (whose255 * return value often depends on the base state). The recipe function is256 * free to mutate its first argument however it wants. All mutations are257 * only ever applied to a __copy__ of the base state.258 *259 * Pass only a function to create a "curried producer" which relieves you260 * from passing the recipe function every time.261 *262 * Only plain objects and arrays are made mutable. All other objects are263 * considered uncopyable.264 *265 * Note: This function is __bound__ to its `Immer` instance.266 *267 * @param {any} base - the initial state268 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified269 * @param {Function} patchListener - optional function that will be called with all the patches produced here270 * @returns {any} a new state, or the initial state if nothing was modified271 */272 produce: IProduce273 /**274 * When true, `produce` will freeze the copies it creates.275 */276 readonly autoFreeze: boolean277 /**278 * When true, drafts are ES2015 proxies.279 */280 readonly useProxies: boolean281 /**282 * Pass true to automatically freeze all copies created by Immer.283 *284 * By default, auto-freezing is disabled in production.285 */286 setAutoFreeze(autoFreeze: boolean): void287 /**288 * Pass true to use the ES2015 `Proxy` class when creating drafts, which is289 * always faster than using ES5 proxies.290 *291 * By default, feature detection is used, so calling this is rarely necessary.292 */293 setUseProxies(useProxies: boolean): voidfix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
lines 294–296why, one step back294}295296export interface ImmerState<T = any> {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 297–300why, one step back297 parent?: ImmerState298 base: T299 copy: T300 assigned: {[prop: string]: boolean; [index: number]: boolean}fix: revert .d.ts and .js.flow module namesaleclarsonbefore adoption44cf843
line 301why, one step back301}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
lines 302–304why, one step back302303// Backward compatibility with --target es5304declare global {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 305–308why, one step back305 interface Set<T> {}306 interface Map<K, V> {}307 interface WeakSet<T> {}308 interface WeakMap<K extends object, V> {}fix(ts): --target=es5 compatibilityAlec Larsonbefore adoption0e5ecc9
line 309why, one step back309}