Skip to content
Myr

types.ts

This file

Path

src/types.ts

185 lines · 5.5 KiB · TypeScript

Last changed by

Make explicit use of TypeScript's ReadonlySet and ReadonlyMap types. Fixes #494.Dave Townsendbefore adoption2fba3f8

History of this file

Take it

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 1–30why, 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 | Promise<any>25 | Date26 | RegExp27 | Boolean28 | Number29 | String30

Make explicit use of TypeScript's ReadonlySet and ReadonlyMap types. Fixes #494.Dave Townsendbefore adoption2fba3f8

lines 31–36why, one step back
31/**
32 * These should also never be mapped but must be tested after regular Map and33 * Set34 */35type WeakReferences = WeakMap<any, any> | WeakSet<any>36

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 37–38why, one step back
37export type Draft<T> = T extends AtomicObject
38 ? T

Make explicit use of TypeScript's ReadonlySet and ReadonlyMap types. Fixes #494.Dave Townsendbefore adoption2fba3f8

lines 39–44why, one step back
39 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap
40 ? Map<Draft<K>, Draft<V>>41 : T extends ReadonlySet<infer V> // Set extends ReadonlySet42 ? Set<Draft<V>>43 : T extends WeakReferences44 ? T

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 45–51why, one step back
45 : T extends object
46 ? {-readonly [K in keyof T]: Draft<T[K]>}47 : T4849/** Convert a mutable type into a readonly type */50export type Immutable<T> = T extends AtomicObject51 ? T

Make explicit use of TypeScript's ReadonlySet and ReadonlyMap types. Fixes #494.Dave Townsendbefore adoption2fba3f8

lines 52–57why, one step back
52 : T extends ReadonlyMap<infer K, infer V> // Map extends ReadonlyMap
53 ? ReadonlyMap<Immutable<K>, Immutable<V>>54 : T extends ReadonlySet<infer V> // Set extends ReadonlySet55 ? ReadonlySet<Immutable<V>>56 : T extends WeakReferences57 ? T

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 58–185why, one step back
58 : T extends object
59 ? {readonly [K in keyof T]: Immutable<T[K]>}60 : T6162export interface Patch {63 op: "replace" | "remove" | "add"64 path: (string | number)[]65 value?: any66}6768export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void6970/** Converts `nothing` into `undefined` */71type FromNothing<T> = T extends Nothing ? undefined : T7273/** The inferred return type of `produce` */74export type Produced<Base, Return> = Return extends void75 ? Base76 : Return extends Promise<infer Result>77 ? Promise<Result extends void ? Base : FromNothing<Result>>78 : FromNothing<Return>7980/**81 * The `produce` function takes a value and a "recipe function" (whose82 * return value often depends on the base state). The recipe function is83 * free to mutate its first argument however it wants. All mutations are84 * only ever applied to a __copy__ of the base state.85 *86 * Pass only a function to create a "curried producer" which relieves you87 * from passing the recipe function every time.88 *89 * Only plain objects and arrays are made mutable. All other objects are90 * considered uncopyable.91 *92 * Note: This function is __bound__ to its `Immer` instance.93 *94 * @param {any} base - the initial state95 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified96 * @param {Function} patchListener - optional function that will be called with all the patches produced here97 * @returns {any} a new state, or the initial state if nothing was modified98 */99export interface IProduce {100 /** Curried producer */101 <102 Recipe extends (...args: any[]) => any,103 Params extends any[] = Parameters<Recipe>,104 T = Params[0]105 >(106 recipe: Recipe107 ): <Base extends Immutable<T>>(108 base: Base,109 ...rest: Tail<Params>110 ) => Produced<Base, ReturnType<Recipe>>111 // ^ by making the returned type generic, the actual type of the passed in object is preferred112 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe113 // 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!114115 /** Curried producer with initial state */116 <117 Recipe extends (...args: any[]) => any,118 Params extends any[] = Parameters<Recipe>,119 T = Params[0]120 >(121 recipe: Recipe,122 initialState: Immutable<T>123 ): <Base extends Immutable<T>>(124 base?: Base,125 ...rest: Tail<Params>126 ) => Produced<Base, ReturnType<Recipe>>127128 /** Normal producer */129 <Base, D = Draft<Base>, Return = void>(130 base: Base,131 recipe: (draft: D) => Return,132 listener?: PatchListener133 ): Produced<Base, Return>134}135136/**137 * Like `produce`, but instead of just returning the new state,138 * a tuple is returned with [nextState, patches, inversePatches]139 *140 * Like produce, this function supports currying141 */142export interface IProduceWithPatches {143 /** Curried producer */144 <145 Recipe extends (...args: any[]) => any,146 Params extends any[] = Parameters<Recipe>,147 T = Params[0]148 >(149 recipe: Recipe150 ): <Base extends Immutable<T>>(151 base: Base,152 ...rest: Tail<Params>153 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]154 // ^ by making the returned type generic, the actual type of the passed in object is preferred155 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe156 // 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!157158 /** Curried producer with initial state */159 <160 Recipe extends (...args: any[]) => any,161 Params extends any[] = Parameters<Recipe>,162 T = Params[0]163 >(164 recipe: Recipe,165 initialState: Immutable<T>166 ): <Base extends Immutable<T>>(167 base?: Base,168 ...rest: Tail<Params>169 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]170171 /** Normal producer */172 <Base, D = Draft<Base>, Return = void>(173 base: Base,174 recipe: (draft: D) => Return175 ): [Produced<Base, Return>, Patch[], Patch[]]176}177178// Backward compatibility with --target es5179// TODO: still needed?180// declare global {181// interface Set<T> {}182// interface Map<K, V> {}183// interface WeakSet<T> {}184// interface WeakMap<K extends object, V> {}185// }