Skip to content
Myr

types.ts

This file

Path

src/types.ts

187 lines · 5.6 KiB · TypeScript

Last changed by

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

History of this file

Take it

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 1–187why, one step back
1import {Nothing} from "./common"
23export type Objectish = any[] | Map<any, any> | Set<any> | {}45export type ObjectishNoSet = any[] | Map<any, any> | {}67export interface ImmerState<T = any> {8 parent?: ImmerState9 base: T10 copy: T11 assigned: {[prop: string]: boolean; [index: number]: boolean}12}1314type Tail<T extends any[]> = ((...t: T) => any) extends ((15 _: any,16 ...tail: infer TT17) => any)18 ? TT19 : []2021/** Object types that should never be mapped */22type AtomicObject =23 | Function24 | WeakMap<any, any>25 | WeakSet<any>26 | Promise<any>27 | Date28 | RegExp29 | Boolean30 | Number31 | String3233export type Draft<T> = T extends AtomicObject34 ? T35 : T extends Map<infer K, infer V>36 ? DraftMap<K, V>37 : T extends Set<infer V>38 ? DraftSet<V>39 : T extends object40 ? {-readonly [K in keyof T]: Draft<T[K]>}41 : T4243// Inline these in ts 3.744interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}4546// Inline these in ts 3.747interface DraftSet<V> extends Set<Draft<V>> {}4849/** Convert a mutable type into a readonly type */50export type Immutable<T> = T extends AtomicObject51 ? T52 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">53 ? ImmutableMap<K, V>54 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">55 ? ImmutableSet<V>56 : T extends object57 ? {readonly [K in keyof T]: Immutable<T[K]>}58 : T5960interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}6162interface ImmutableSet<V> extends Set<Immutable<V>> {}6364export interface Patch {65 op: "replace" | "remove" | "add"66 path: (string | number)[]67 value?: any68}6970export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7172/** Converts `nothing` into `undefined` */73type FromNothing<T> = T extends Nothing ? undefined : T7475/** The inferred return type of `produce` */76export type Produced<Base, Return> = Return extends void77 ? Base78 : Return extends Promise<infer Result>79 ? Promise<Result extends void ? Base : FromNothing<Result>>80 : FromNothing<Return>8182/**83 * The `produce` function takes a value and a "recipe function" (whose84 * return value often depends on the base state). The recipe function is85 * free to mutate its first argument however it wants. All mutations are86 * only ever applied to a __copy__ of the base state.87 *88 * Pass only a function to create a "curried producer" which relieves you89 * from passing the recipe function every time.90 *91 * Only plain objects and arrays are made mutable. All other objects are92 * considered uncopyable.93 *94 * Note: This function is __bound__ to its `Immer` instance.95 *96 * @param {any} base - the initial state97 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified98 * @param {Function} patchListener - optional function that will be called with all the patches produced here99 * @returns {any} a new state, or the initial state if nothing was modified100 */101export interface IProduce {102 /** Curried producer */103 <104 Recipe extends (...args: any[]) => any,105 Params extends any[] = Parameters<Recipe>,106 T = Params[0]107 >(108 recipe: Recipe109 ): <Base extends Immutable<T>>(110 base: Base,111 ...rest: Tail<Params>112 ) => Produced<Base, ReturnType<Recipe>>113 // ^ by making the returned type generic, the actual type of the passed in object is preferred114 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe115 // 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!116117 /** Curried producer with initial state */118 <119 Recipe extends (...args: any[]) => any,120 Params extends any[] = Parameters<Recipe>,121 T = Params[0]122 >(123 recipe: Recipe,124 initialState: Immutable<T>125 ): <Base extends Immutable<T>>(126 base?: Base,127 ...rest: Tail<Params>128 ) => Produced<Base, ReturnType<Recipe>>129130 /** Normal producer */131 <Base, D = Draft<Base>, Return = void>(132 base: Base,133 recipe: (draft: D) => Return,134 listener?: PatchListener135 ): Produced<Base, Return>136}137138/**139 * Like `produce`, but instead of just returning the new state,140 * a tuple is returned with [nextState, patches, inversePatches]141 *142 * Like produce, this function supports currying143 */144export interface IProduceWithPatches {145 /** Curried producer */146 <147 Recipe extends (...args: any[]) => any,148 Params extends any[] = Parameters<Recipe>,149 T = Params[0]150 >(151 recipe: Recipe152 ): <Base extends Immutable<T>>(153 base: Base,154 ...rest: Tail<Params>155 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]156 // ^ by making the returned type generic, the actual type of the passed in object is preferred157 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe158 // 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!159160 /** Curried producer with initial state */161 <162 Recipe extends (...args: any[]) => any,163 Params extends any[] = Parameters<Recipe>,164 T = Params[0]165 >(166 recipe: Recipe,167 initialState: Immutable<T>168 ): <Base extends Immutable<T>>(169 base?: Base,170 ...rest: Tail<Params>171 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]172173 /** Normal producer */174 <Base, D = Draft<Base>, Return = void>(175 base: Base,176 recipe: (draft: D) => Return177 ): [Produced<Base, Return>, Patch[], Patch[]]178}179180// Backward compatibility with --target es5181// TODO: still needed?182// declare global {183// interface Set<T> {}184// interface Map<K, V> {}185// interface WeakSet<T> {}186// interface WeakMap<K extends object, V> {}187// }