types.ts
This file
Path
src/types.ts
216 lines · 6.2 KiB · TypeScript
Last changed by
Introduced proper state typesMichel Weststratebefore adoption3842f85
History of this fileTake it
Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 1–5why, one step back1import {Nothing, DRAFT_STATE} from "./common"2import {SetState} from "./set"3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 6–10why, one step back67export type Objectish = any[] | Map<any, any> | Set<any> | {}89export type ObjectishNoSet = any[] | Map<any, any> | {}10Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 11–41why, one step back11// export interface ImmerState<T = any> {12// parent?: ImmerState13// base: T14// copy: T15// assigned: {[prop: string]: boolean; [index: number]: boolean}16// }1718export type AnyObject = {[key: string]: any}19export type AnyArray = Array<any>20export type AnySet = Set<any>21export type AnyMap = Map<any, any>2223export type DraftType =24 | "proxy_object"25 | "proxy_array"26 | "es5_object"27 | "es5_array"28 | "map"29 | "set"3031export type ImmerState =32 | ProxyObjectState33 | ProxyArrayState34 | ES5ObjectState35 | ES5ArrayState36 | MapState37 | SetState3839export type Drafted<Base, T extends ImmerState> = {40 [DRAFT_STATE]: T41} & BaseMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 42–216why, one step back4243type Tail<T extends any[]> = ((...t: T) => any) extends ((44 _: any,45 ...tail: infer TT46) => any)47 ? TT48 : []4950/** Object types that should never be mapped */51type AtomicObject =52 | Function53 | WeakMap<any, any>54 | WeakSet<any>55 | Promise<any>56 | Date57 | RegExp58 | Boolean59 | Number60 | String6162export type Draft<T> = T extends AtomicObject63 ? T64 : T extends Map<infer K, infer V>65 ? DraftMap<K, V>66 : T extends Set<infer V>67 ? DraftSet<V>68 : T extends object69 ? {-readonly [K in keyof T]: Draft<T[K]>}70 : T7172// Inline these in ts 3.773interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}7475// Inline these in ts 3.776interface DraftSet<V> extends Set<Draft<V>> {}7778/** Convert a mutable type into a readonly type */79export type Immutable<T> = T extends AtomicObject80 ? T81 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">82 ? ImmutableMap<K, V>83 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">84 ? ImmutableSet<V>85 : T extends object86 ? {readonly [K in keyof T]: Immutable<T[K]>}87 : T8889interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}9091interface ImmutableSet<V> extends Set<Immutable<V>> {}9293export interface Patch {94 op: "replace" | "remove" | "add"95 path: (string | number)[]96 value?: any97}9899export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void100101/** Converts `nothing` into `undefined` */102type FromNothing<T> = T extends Nothing ? undefined : T103104/** The inferred return type of `produce` */105export type Produced<Base, Return> = Return extends void106 ? Base107 : Return extends Promise<infer Result>108 ? Promise<Result extends void ? Base : FromNothing<Result>>109 : FromNothing<Return>110111/**112 * The `produce` function takes a value and a "recipe function" (whose113 * return value often depends on the base state). The recipe function is114 * free to mutate its first argument however it wants. All mutations are115 * only ever applied to a __copy__ of the base state.116 *117 * Pass only a function to create a "curried producer" which relieves you118 * from passing the recipe function every time.119 *120 * Only plain objects and arrays are made mutable. All other objects are121 * considered uncopyable.122 *123 * Note: This function is __bound__ to its `Immer` instance.124 *125 * @param {any} base - the initial state126 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified127 * @param {Function} patchListener - optional function that will be called with all the patches produced here128 * @returns {any} a new state, or the initial state if nothing was modified129 */130export interface IProduce {131 /** Curried producer */132 <133 Recipe extends (...args: any[]) => any,134 Params extends any[] = Parameters<Recipe>,135 T = Params[0]136 >(137 recipe: Recipe138 ): <Base extends Immutable<T>>(139 base: Base,140 ...rest: Tail<Params>141 ) => Produced<Base, ReturnType<Recipe>>142 // ^ by making the returned type generic, the actual type of the passed in object is preferred143 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe144 // 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!145146 /** Curried producer with initial state */147 <148 Recipe extends (...args: any[]) => any,149 Params extends any[] = Parameters<Recipe>,150 T = Params[0]151 >(152 recipe: Recipe,153 initialState: Immutable<T>154 ): <Base extends Immutable<T>>(155 base?: Base,156 ...rest: Tail<Params>157 ) => Produced<Base, ReturnType<Recipe>>158159 /** Normal producer */160 <Base, D = Draft<Base>, Return = void>(161 base: Base,162 recipe: (draft: D) => Return,163 listener?: PatchListener164 ): Produced<Base, Return>165}166167/**168 * Like `produce`, but instead of just returning the new state,169 * a tuple is returned with [nextState, patches, inversePatches]170 *171 * Like produce, this function supports currying172 */173export interface IProduceWithPatches {174 /** Curried producer */175 <176 Recipe extends (...args: any[]) => any,177 Params extends any[] = Parameters<Recipe>,178 T = Params[0]179 >(180 recipe: Recipe181 ): <Base extends Immutable<T>>(182 base: Base,183 ...rest: Tail<Params>184 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]185 // ^ by making the returned type generic, the actual type of the passed in object is preferred186 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe187 // 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!188189 /** Curried producer with initial state */190 <191 Recipe extends (...args: any[]) => any,192 Params extends any[] = Parameters<Recipe>,193 T = Params[0]194 >(195 recipe: Recipe,196 initialState: Immutable<T>197 ): <Base extends Immutable<T>>(198 base?: Base,199 ...rest: Tail<Params>200 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]201202 /** Normal producer */203 <Base, D = Draft<Base>, Return = void>(204 base: Base,205 recipe: (draft: D) => Return206 ): [Produced<Base, Return>, Patch[], Patch[]]207}208209// Backward compatibility with --target es5210// TODO: still needed?211// declare global {212// interface Set<T> {}213// interface Map<K, V> {}214// interface WeakSet<T> {}215// interface WeakMap<K extends object, V> {}216// }