types.ts
This file
Path
src/types.ts
209 lines · 6.2 KiB · TypeScript
Last changed by
TS type improvementsMichel Weststratebefore adoptiondd1808e
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
line 6why, one step back6Dropped Object.assignMichel Weststratebefore adoption79a1d2c
lines 7–8why, one step back7export type Objectish = AnyObject | AnyArray | AnyMap | AnySet8export type ObjectishNoSet = AnyObject | AnyArray | AnyMapMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 9why, one step back9Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 10–30why, one step back10export type AnyObject = {[key: string]: any}11export type AnyArray = Array<any>12export type AnySet = Set<any>13export type AnyMap = Map<any, any>1415export type DraftType =16 | "proxy_object"17 | "proxy_array"18 | "es5_object"19 | "es5_array"20 | "map"21 | "set"2223export type ImmerState =24 | ProxyObjectState25 | ProxyArrayState26 | ES5ObjectState27 | ES5ArrayState28 | MapState29 | SetState30TS type improvementsMichel Weststratebefore adoptiondd1808e
line 31why, one step back31// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)clean up create proxyMichel Weststratebefore adoptionc182c1e
line 32why, one step back32export type Drafted<Base = any, T extends ImmerState = ImmerState> = {Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 33–34why, one step back33 [DRAFT_STATE]: T34} & BaseMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 35why, one step back35clean up create proxyMichel Weststratebefore adoptionc182c1e
line 36why, one step back36type Tail<T extends any[]> = ((...t: T) => any) extends (Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 37–38why, one step back37 _: any,38 ...tail: infer TTclean up create proxyMichel Weststratebefore adoptionc182c1e
line 39why, one step back39) => anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 40–209why, one step back40 ? TT41 : []4243/** Object types that should never be mapped */44type AtomicObject =45 | Function46 | WeakMap<any, any>47 | WeakSet<any>48 | Promise<any>49 | Date50 | RegExp51 | Boolean52 | Number53 | String5455export type Draft<T> = T extends AtomicObject56 ? T57 : T extends Map<infer K, infer V>58 ? DraftMap<K, V>59 : T extends Set<infer V>60 ? DraftSet<V>61 : T extends object62 ? {-readonly [K in keyof T]: Draft<T[K]>}63 : T6465// Inline these in ts 3.766interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}6768// Inline these in ts 3.769interface DraftSet<V> extends Set<Draft<V>> {}7071/** Convert a mutable type into a readonly type */72export type Immutable<T> = T extends AtomicObject73 ? T74 : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">75 ? ImmutableMap<K, V>76 : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">77 ? ImmutableSet<V>78 : T extends object79 ? {readonly [K in keyof T]: Immutable<T[K]>}80 : T8182interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}8384interface ImmutableSet<V> extends Set<Immutable<V>> {}8586export interface Patch {87 op: "replace" | "remove" | "add"88 path: (string | number)[]89 value?: any90}9192export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void9394/** Converts `nothing` into `undefined` */95type FromNothing<T> = T extends Nothing ? undefined : T9697/** The inferred return type of `produce` */98export type Produced<Base, Return> = Return extends void99 ? Base100 : Return extends Promise<infer Result>101 ? Promise<Result extends void ? Base : FromNothing<Result>>102 : FromNothing<Return>103104/**105 * The `produce` function takes a value and a "recipe function" (whose106 * return value often depends on the base state). The recipe function is107 * free to mutate its first argument however it wants. All mutations are108 * only ever applied to a __copy__ of the base state.109 *110 * Pass only a function to create a "curried producer" which relieves you111 * from passing the recipe function every time.112 *113 * Only plain objects and arrays are made mutable. All other objects are114 * considered uncopyable.115 *116 * Note: This function is __bound__ to its `Immer` instance.117 *118 * @param {any} base - the initial state119 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified120 * @param {Function} patchListener - optional function that will be called with all the patches produced here121 * @returns {any} a new state, or the initial state if nothing was modified122 */123export interface IProduce {124 /** Curried producer */125 <126 Recipe extends (...args: any[]) => any,127 Params extends any[] = Parameters<Recipe>,128 T = Params[0]129 >(130 recipe: Recipe131 ): <Base extends Immutable<T>>(132 base: Base,133 ...rest: Tail<Params>134 ) => Produced<Base, ReturnType<Recipe>>135 // ^ by making the returned type generic, the actual type of the passed in object is preferred136 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe137 // 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!138139 /** Curried producer with initial state */140 <141 Recipe extends (...args: any[]) => any,142 Params extends any[] = Parameters<Recipe>,143 T = Params[0]144 >(145 recipe: Recipe,146 initialState: Immutable<T>147 ): <Base extends Immutable<T>>(148 base?: Base,149 ...rest: Tail<Params>150 ) => Produced<Base, ReturnType<Recipe>>151152 /** Normal producer */153 <Base, D = Draft<Base>, Return = void>(154 base: Base,155 recipe: (draft: D) => Return,156 listener?: PatchListener157 ): Produced<Base, Return>158}159160/**161 * Like `produce`, but instead of just returning the new state,162 * a tuple is returned with [nextState, patches, inversePatches]163 *164 * Like produce, this function supports currying165 */166export interface IProduceWithPatches {167 /** Curried producer */168 <169 Recipe extends (...args: any[]) => any,170 Params extends any[] = Parameters<Recipe>,171 T = Params[0]172 >(173 recipe: Recipe174 ): <Base extends Immutable<T>>(175 base: Base,176 ...rest: Tail<Params>177 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]178 // ^ by making the returned type generic, the actual type of the passed in object is preferred179 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe180 // 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!181182 /** Curried producer with initial state */183 <184 Recipe extends (...args: any[]) => any,185 Params extends any[] = Parameters<Recipe>,186 T = Params[0]187 >(188 recipe: Recipe,189 initialState: Immutable<T>190 ): <Base extends Immutable<T>>(191 base?: Base,192 ...rest: Tail<Params>193 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]194195 /** Normal producer */196 <Base, D = Draft<Base>, Return = void>(197 base: Base,198 recipe: (draft: D) => Return199 ): [Produced<Base, Return>, Patch[], Patch[]]200}201202// Backward compatibility with --target es5203// TODO: still needed?204// declare global {205// interface Set<T> {}206// interface Map<K, V> {}207// interface WeakSet<T> {}208// interface WeakMap<K extends object, V> {}209// }