Skip to content
Myr

types-external.ts

This file

Path

src/types/types-external.ts

171 lines · 5.2 KiB · TypeScript

Last changed by

Replace anonymous type in recursive Draft declaration for better tsc performance in watch mode.Ivan Buryakbefore adoption9bea756

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–25why, one step back
1import {Nothing} from "../internal"
23type Tail<T extends any[]> = ((...t: T) => any) extends (4 _: any,5 ...tail: infer TT6) => any7 ? TT8 : []910/** Object types that should never be mapped */11type AtomicObject =12 | Function13 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String1920/**21 * These should also never be mapped but must be tested after regular Map and22 * Set23 */24type WeakReferences = WeakMap<any, any> | WeakSet<any>25

Replace anonymous type in recursive Draft declaration for better tsc performance in watch mode.Ivan Buryakbefore adoption9bea756

lines 26–27why, one step back
26export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}
27

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 28–36why, one step back
28export type Draft<T> = T extends AtomicObject
29 ? T30 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap31 ? Map<Draft<K>, Draft<V>>32 : T extends ReadonlySet<infer V> // Set extends ReadonlySet33 ? Set<Draft<V>>34 : T extends WeakReferences35 ? T36 : T extends object

Replace anonymous type in recursive Draft declaration for better tsc performance in watch mode.Ivan Buryakbefore adoption9bea756

line 37why, one step back
37 ? WritableDraft<T>

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 38–171why, one step back
38 : T
3940/** Convert a mutable type into a readonly type */41export type Immutable<T> = T extends AtomicObject42 ? T43 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap44 ? ReadonlyMap<Immutable<K>, Immutable<V>>45 : T extends ReadonlySet<infer V> // Set extends ReadonlySet46 ? ReadonlySet<Immutable<V>>47 : T extends WeakReferences48 ? T49 : T extends object50 ? {readonly [K in keyof T]: Immutable<T[K]>}51 : T5253export interface Patch {54 op: "replace" | "remove" | "add"55 path: (string | number)[]56 value?: any57}5859export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void6061/** Converts `nothing` into `undefined` */62type FromNothing<T> = T extends Nothing ? undefined : T6364/** The inferred return type of `produce` */65export type Produced<Base, Return> = Return extends void66 ? Base67 : Return extends Promise<infer Result>68 ? Promise<Result extends void ? Base : FromNothing<Result>>69 : FromNothing<Return>7071/**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 */90export interface IProduce {91 /** 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!105106 /** 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>>118119 /** Normal producer */120 <Base, D = Draft<Base>, Return = void>(121 base: Base,122 recipe: (draft: D) => Return,123 listener?: PatchListener124 ): Produced<Base, Return>125}126127/**128 * Like `produce`, but instead of just returning the new state,129 * a tuple is returned with [nextState, patches, inversePatches]130 *131 * Like produce, this function supports currying132 */133export interface IProduceWithPatches {134 /** Curried producer */135 <136 Recipe extends (...args: any[]) => any,137 Params extends any[] = Parameters<Recipe>,138 T = Params[0]139 >(140 recipe: Recipe141 ): <Base extends Immutable<T>>(142 base: Base,143 ...rest: Tail<Params>144 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]145 // ^ by making the returned type generic, the actual type of the passed in object is preferred146 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe147 // 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!148149 /** Curried producer with initial state */150 <151 Recipe extends (...args: any[]) => any,152 Params extends any[] = Parameters<Recipe>,153 T = Params[0]154 >(155 recipe: Recipe,156 initialState: Immutable<T>157 ): <Base extends Immutable<T>>(158 base?: Base,159 ...rest: Tail<Params>160 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]161162 /** Normal producer */163 <Base, D = Draft<Base>, Return = void>(164 base: Base,165 recipe: (draft: D) => Return166 ): [Produced<Base, Return>, Patch[], Patch[]]167}168169// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..170// hopefully it get's tree-shaken away for everyone :)171export function never_used() {}