types-external.ts
This file
Path
src/types-external.ts
165 lines · 5.0 KiB · TypeScript
Last changed by
feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
History of this fileTake it
feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 1–165why, one step back1import {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>2526export type Draft<T> = T extends AtomicObject27 ? T28 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap29 ? Map<Draft<K>, Draft<V>>30 : T extends ReadonlySet<infer V> // Set extends ReadonlySet31 ? Set<Draft<V>>32 : T extends WeakReferences33 ? T34 : T extends object35 ? {-readonly [K in keyof T]: Draft<T[K]>}36 : T3738/** Convert a mutable type into a readonly type */39export type Immutable<T> = T extends AtomicObject40 ? T41 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap42 ? ReadonlyMap<Immutable<K>, Immutable<V>>43 : T extends ReadonlySet<infer V> // Set extends ReadonlySet44 ? ReadonlySet<Immutable<V>>45 : T extends WeakReferences46 ? T47 : T extends object48 ? {readonly [K in keyof T]: Immutable<T[K]>}49 : T5051export interface Patch {52 op: "replace" | "remove" | "add"53 path: (string | number)[]54 value?: any55}5657export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void5859/** Converts `nothing` into `undefined` */60type FromNothing<T> = T extends Nothing ? undefined : T6162/** The inferred return type of `produce` */63export type Produced<Base, Return> = Return extends void64 ? Base65 : Return extends Promise<infer Result>66 ? Promise<Result extends void ? Base : FromNothing<Result>>67 : FromNothing<Return>6869/**70 * The `produce` function takes a value and a "recipe function" (whose71 * return value often depends on the base state). The recipe function is72 * free to mutate its first argument however it wants. All mutations are73 * only ever applied to a __copy__ of the base state.74 *75 * Pass only a function to create a "curried producer" which relieves you76 * from passing the recipe function every time.77 *78 * Only plain objects and arrays are made mutable. All other objects are79 * considered uncopyable.80 *81 * Note: This function is __bound__ to its `Immer` instance.82 *83 * @param {any} base - the initial state84 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified85 * @param {Function} patchListener - optional function that will be called with all the patches produced here86 * @returns {any} a new state, or the initial state if nothing was modified87 */88export interface IProduce {89 /** Curried producer */90 <91 Recipe extends (...args: any[]) => any,92 Params extends any[] = Parameters<Recipe>,93 T = Params[0]94 >(95 recipe: Recipe96 ): <Base extends Immutable<T>>(97 base: Base,98 ...rest: Tail<Params>99 ) => Produced<Base, ReturnType<Recipe>>100 // ^ by making the returned type generic, the actual type of the passed in object is preferred101 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe102 // 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!103104 /** Curried producer with initial state */105 <106 Recipe extends (...args: any[]) => any,107 Params extends any[] = Parameters<Recipe>,108 T = Params[0]109 >(110 recipe: Recipe,111 initialState: Immutable<T>112 ): <Base extends Immutable<T>>(113 base?: Base,114 ...rest: Tail<Params>115 ) => Produced<Base, ReturnType<Recipe>>116117 /** Normal producer */118 <Base, D = Draft<Base>, Return = void>(119 base: Base,120 recipe: (draft: D) => Return,121 listener?: PatchListener122 ): Produced<Base, Return>123}124125/**126 * Like `produce`, but instead of just returning the new state,127 * a tuple is returned with [nextState, patches, inversePatches]128 *129 * Like produce, this function supports currying130 */131export interface IProduceWithPatches {132 /** Curried producer */133 <134 Recipe extends (...args: any[]) => any,135 Params extends any[] = Parameters<Recipe>,136 T = Params[0]137 >(138 recipe: Recipe139 ): <Base extends Immutable<T>>(140 base: Base,141 ...rest: Tail<Params>142 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]143 // ^ by making the returned type generic, the actual type of the passed in object is preferred144 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe145 // 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!146147 /** Curried producer with initial state */148 <149 Recipe extends (...args: any[]) => any,150 Params extends any[] = Parameters<Recipe>,151 T = Params[0]152 >(153 recipe: Recipe,154 initialState: Immutable<T>155 ): <Base extends Immutable<T>>(156 base?: Base,157 ...rest: Tail<Params>158 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]159160 /** Normal producer */161 <Base, D = Draft<Base>, Return = void>(162 base: Base,163 recipe: (draft: D) => Return164 ): [Produced<Base, Return>, Patch[], Patch[]]165}