Skip to content
Myr

types.ts

This file

Path

src/types.ts

225 lines · 6.4 KiB · TypeScript

Last changed by

Enums FTWMichel Weststratebefore adoptionfd76f1c

History of this file

Take it

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 1–5why, one step back
1import {Nothing, DRAFT_STATE} from "./common"
2import {SetState} from "./set"3import {MapState} from "./map"4import {ProxyObjectState, ProxyArrayState} from "./proxy"5import {ES5ObjectState, ES5ArrayState} from "./es5"

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 6why, one step back
6import {ImmerScope} from "./scope"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 7why, one step back
7

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

lines 8–9why, one step back
8export type Objectish = AnyObject | AnyArray | AnyMap | AnySet
9export type ObjectishNoSet = AnyObject | AnyArray | AnyMap

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 10why, one step back
10

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 11–14why, one step back
11export type AnyObject = {[key: string]: any}
12export type AnyArray = Array<any>13export type AnySet = Set<any>14export type AnyMap = Map<any, any>

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 15–20why, one step back
15export enum Archtype {
16 Object,17 Array,18 Map,19 Set20}

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 21why, one step back
21

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 22–29why, one step back
22export enum ProxyType {
23 ProxyObject,24 ProxyArray,25 ES5Object,26 ES5Array,27 Map,28 Set29}

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 30–37why, one step back
30
31export interface ImmerBaseState {32 parent?: ImmerState33 scope: ImmerScope34 modified: boolean35 finalized: boolean36 isManual: boolean37}

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 38–46why, one step back
38
39export type ImmerState =40 | ProxyObjectState41 | ProxyArrayState42 | ES5ObjectState43 | ES5ArrayState44 | MapState45 | SetState46

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 47why, one step back
47// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 48why, one step back
48export type Drafted<Base = any, T extends ImmerState = ImmerState> = {

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 49–50why, one step back
49 [DRAFT_STATE]: T
50} & Base

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 51why, one step back
51

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 52why, one step back
52type Tail<T extends any[]> = ((...t: T) => any) extends (

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 53–54why, one step back
53 _: any,
54 ...tail: infer TT

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 55why, one step back
55) => any

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 56–225why, one step back
56 ? TT
57 : []5859/** Object types that should never be mapped */60type AtomicObject =61 | Function62 | WeakMap<any, any>63 | WeakSet<any>64 | Promise<any>65 | Date66 | RegExp67 | Boolean68 | Number69 | String7071export type Draft<T> = T extends AtomicObject72 ? T73 : T extends Map<infer K, infer V>74 ? DraftMap<K, V>75 : T extends Set<infer V>76 ? DraftSet<V>77 : T extends object78 ? {-readonly [K in keyof T]: Draft<T[K]>}79 : T8081// Inline these in ts 3.782interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}8384// Inline these in ts 3.785interface DraftSet<V> extends Set<Draft<V>> {}8687/** Convert a mutable type into a readonly type */88export type Immutable<T> = T extends AtomicObject89 ? T90 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">91 ? ImmutableMap<K, V>92 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">93 ? ImmutableSet<V>94 : T extends object95 ? {readonly [K in keyof T]: Immutable<T[K]>}96 : T9798interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}99100interface ImmutableSet<V> extends Set<Immutable<V>> {}101102export interface Patch {103 op: "replace" | "remove" | "add"104 path: (string | number)[]105 value?: any106}107108export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void109110/** Converts `nothing` into `undefined` */111type FromNothing<T> = T extends Nothing ? undefined : T112113/** The inferred return type of `produce` */114export type Produced<Base, Return> = Return extends void115 ? Base116 : Return extends Promise<infer Result>117 ? Promise<Result extends void ? Base : FromNothing<Result>>118 : FromNothing<Return>119120/**121 * The `produce` function takes a value and a "recipe function" (whose122 * return value often depends on the base state). The recipe function is123 * free to mutate its first argument however it wants. All mutations are124 * only ever applied to a __copy__ of the base state.125 *126 * Pass only a function to create a "curried producer" which relieves you127 * from passing the recipe function every time.128 *129 * Only plain objects and arrays are made mutable. All other objects are130 * considered uncopyable.131 *132 * Note: This function is __bound__ to its `Immer` instance.133 *134 * @param {any} base - the initial state135 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified136 * @param {Function} patchListener - optional function that will be called with all the patches produced here137 * @returns {any} a new state, or the initial state if nothing was modified138 */139export interface IProduce {140 /** Curried producer */141 <142 Recipe extends (...args: any[]) => any,143 Params extends any[] = Parameters<Recipe>,144 T = Params[0]145 >(146 recipe: Recipe147 ): <Base extends Immutable<T>>(148 base: Base,149 ...rest: Tail<Params>150 ) => Produced<Base, ReturnType<Recipe>>151 // ^ by making the returned type generic, the actual type of the passed in object is preferred152 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe153 // 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!154155 /** Curried producer with initial state */156 <157 Recipe extends (...args: any[]) => any,158 Params extends any[] = Parameters<Recipe>,159 T = Params[0]160 >(161 recipe: Recipe,162 initialState: Immutable<T>163 ): <Base extends Immutable<T>>(164 base?: Base,165 ...rest: Tail<Params>166 ) => Produced<Base, ReturnType<Recipe>>167168 /** Normal producer */169 <Base, D = Draft<Base>, Return = void>(170 base: Base,171 recipe: (draft: D) => Return,172 listener?: PatchListener173 ): Produced<Base, Return>174}175176/**177 * Like `produce`, but instead of just returning the new state,178 * a tuple is returned with [nextState, patches, inversePatches]179 *180 * Like produce, this function supports currying181 */182export interface IProduceWithPatches {183 /** Curried producer */184 <185 Recipe extends (...args: any[]) => any,186 Params extends any[] = Parameters<Recipe>,187 T = Params[0]188 >(189 recipe: Recipe190 ): <Base extends Immutable<T>>(191 base: Base,192 ...rest: Tail<Params>193 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]194 // ^ by making the returned type generic, the actual type of the passed in object is preferred195 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe196 // 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!197198 /** Curried producer with initial state */199 <200 Recipe extends (...args: any[]) => any,201 Params extends any[] = Parameters<Recipe>,202 T = Params[0]203 >(204 recipe: Recipe,205 initialState: Immutable<T>206 ): <Base extends Immutable<T>>(207 base?: Base,208 ...rest: Tail<Params>209 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]210211 /** Normal producer */212 <Base, D = Draft<Base>, Return = void>(213 base: Base,214 recipe: (draft: D) => Return215 ): [Produced<Base, Return>, Patch[], Patch[]]216}217218// Backward compatibility with --target es5219// TODO: still needed?220// declare global {221// interface Set<T> {}222// interface Map<K, V> {}223// interface WeakSet<T> {}224// interface WeakMap<K extends object, V> {}225// }