splitted types
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 9f4711a · Browse files · 4 files·+224 added−222 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+224 added−222 removed
- other:+224 added−222 removed
Provenance
- Commit
71f67e5- Tree
58981c1- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
9f4711a
4 files changed+224 added−222 removed
modified:src/internal.ts+2 added-1 removed
1233456785 unmodified linesexport * from "./env"export * from "./extends"export * from "./types"export * from "./types-external"export * from "./types-internal"export * from "./common"export * from "./scope"export * from "./finalize"export * from "./proxy"5 unmodified linesadded:src/types-external.ts+167 added
1+import {Nothing} from "./internal"2+3+type Tail<T extends any[]> = ((...t: T) => any) extends (4+ _: any,5+ ...tail: infer TT6+) => any7+ ? TT8+ : []9+10+/** Object types that should never be mapped */11+type AtomicObject =12+ | Function13+ | WeakMap<any, any>14+ | WeakSet<any>15+ | Promise<any>16+ | Date17+ | RegExp18+ | Boolean19+ | Number20+ | String21+22+export type Draft<T> = T extends AtomicObject23+ ? T24+ : T extends Map<infer K, infer V>25+ ? DraftMap<K, V>26+ : T extends Set<infer V>27+ ? DraftSet<V>28+ : T extends object29+ ? {-readonly [K in keyof T]: Draft<T[K]>}30+ : T31+32+// Inline these in ts 3.733+interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}34+35+// Inline these in ts 3.736+interface DraftSet<V> extends Set<Draft<V>> {}37+38+/** Convert a mutable type into a readonly type */39+export type Immutable<T> = T extends AtomicObject40+ ? T41+ : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">42+ ? ImmutableMap<K, V>43+ : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">44+ ? ImmutableSet<V>45+ : T extends object46+ ? {readonly [K in keyof T]: Immutable<T[K]>}47+ : T48+49+interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}50+51+interface ImmutableSet<V> extends Set<Immutable<V>> {}52+53+export interface Patch {54+ op: "replace" | "remove" | "add"55+ path: (string | number)[]56+ value?: any57+}58+59+export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void60+61+/** Converts `nothing` into `undefined` */62+type FromNothing<T> = T extends Nothing ? undefined : T63+64+/** The inferred return type of `produce` */65+export type Produced<Base, Return> = Return extends void66+ ? Base67+ : Return extends Promise<infer Result>68+ ? Promise<Result extends void ? Base : FromNothing<Result>>69+ : FromNothing<Return>70+71+/**72+ * The `produce` function takes a value and a "recipe function" (whose73+ * return value often depends on the base state). The recipe function is74+ * free to mutate its first argument however it wants. All mutations are75+ * only ever applied to a __copy__ of the base state.76+ *77+ * Pass only a function to create a "curried producer" which relieves you78+ * from passing the recipe function every time.79+ *80+ * Only plain objects and arrays are made mutable. All other objects are81+ * considered uncopyable.82+ *83+ * Note: This function is __bound__ to its `Immer` instance.84+ *85+ * @param {any} base - the initial state86+ * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified87+ * @param {Function} patchListener - optional function that will be called with all the patches produced here88+ * @returns {any} a new state, or the initial state if nothing was modified89+ */90+export interface IProduce {91+ /** Curried producer */92+ <93+ Recipe extends (...args: any[]) => any,94+ Params extends any[] = Parameters<Recipe>,95+ T = Params[0]96+ >(97+ recipe: Recipe98+ ): <Base extends Immutable<T>>(99+ base: Base,100+ ...rest: Tail<Params>101+ ) => Produced<Base, ReturnType<Recipe>>102+ // ^ by making the returned type generic, the actual type of the passed in object is preferred103+ // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe104+ // 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!105+106+ /** Curried producer with initial state */107+ <108+ Recipe extends (...args: any[]) => any,109+ Params extends any[] = Parameters<Recipe>,110+ T = Params[0]111+ >(112+ recipe: Recipe,113+ initialState: Immutable<T>114+ ): <Base extends Immutable<T>>(115+ base?: Base,116+ ...rest: Tail<Params>117+ ) => Produced<Base, ReturnType<Recipe>>118+119+ /** Normal producer */120+ <Base, D = Draft<Base>, Return = void>(121+ base: Base,122+ recipe: (draft: D) => Return,123+ listener?: PatchListener124+ ): Produced<Base, Return>125+}126+127+/**128+ * Like `produce`, but instead of just returning the new state,129+ * a tuple is returned with [nextState, patches, inversePatches]130+ *131+ * Like produce, this function supports currying132+ */133+export interface IProduceWithPatches {134+ /** Curried producer */135+ <136+ Recipe extends (...args: any[]) => any,137+ Params extends any[] = Parameters<Recipe>,138+ T = Params[0]139+ >(140+ recipe: Recipe141+ ): <Base extends Immutable<T>>(142+ base: Base,143+ ...rest: Tail<Params>144+ ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]145+ // ^ by making the returned type generic, the actual type of the passed in object is preferred146+ // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe147+ // 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!148+149+ /** Curried producer with initial state */150+ <151+ Recipe extends (...args: any[]) => any,152+ Params extends any[] = Parameters<Recipe>,153+ T = Params[0]154+ >(155+ recipe: Recipe,156+ initialState: Immutable<T>157+ ): <Base extends Immutable<T>>(158+ base?: Base,159+ ...rest: Tail<Params>160+ ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]161+162+ /** Normal producer */163+ <Base, D = Draft<Base>, Return = void>(164+ base: Base,165+ recipe: (draft: D) => Return166+ ): [Produced<Base, Return>, Patch[], Patch[]]167+}added:src/types-internal.ts+55 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import { SetState, ImmerScope, ProxyObjectState, ProxyArrayState, ES5ObjectState, ES5ArrayState, MapState, DRAFT_STATE, Nothing} from "./internal"
export type Objectish = AnyObject | AnyArray | AnyMap | AnySetexport type ObjectishNoSet = AnyObject | AnyArray | AnyMap
export type AnyObject = {[key: string]: any}export type AnyArray = Array<any>export type AnySet = Set<any>export type AnyMap = Map<any, any>export enum Archtype { Object, Array, Map, Set}
export enum ProxyType { ProxyObject, ProxyArray, ES5Object, ES5Array, Map, Set}
export interface ImmerBaseState { parent?: ImmerState scope: ImmerScope modified: boolean finalized: boolean isManual: boolean}
export type ImmerState = | ProxyObjectState | ProxyArrayState | ES5ObjectState | ES5ArrayState | MapState | SetState
// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)export type Drafted<Base = any, T extends ImmerState = ImmerState> = { [DRAFT_STATE]: T} & Basedeleted:src/types.ts-221 removed
1-import {2- SetState,3- ImmerScope,4- ProxyObjectState,5- ProxyArrayState,6- ES5ObjectState,7- ES5ArrayState,8- MapState,9- DRAFT_STATE,10- Nothing11-} from "./internal"12-13-export type Objectish = AnyObject | AnyArray | AnyMap | AnySet14-export type ObjectishNoSet = AnyObject | AnyArray | AnyMap15-16-export type AnyObject = {[key: string]: any}17-export type AnyArray = Array<any>18-export type AnySet = Set<any>19-export type AnyMap = Map<any, any>20-export enum Archtype {21- Object,22- Array,23- Map,24- Set25-}26-27-export enum ProxyType {28- ProxyObject,29- ProxyArray,30- ES5Object,31- ES5Array,32- Map,33- Set34-}35-36-export interface ImmerBaseState {37- parent?: ImmerState38- scope: ImmerScope39- modified: boolean40- finalized: boolean41- isManual: boolean42-}43-44-export type ImmerState =45- | ProxyObjectState46- | ProxyArrayState47- | ES5ObjectState48- | ES5ArrayState49- | MapState50- | SetState51-52-// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)53-export type Drafted<Base = any, T extends ImmerState = ImmerState> = {54- [DRAFT_STATE]: T55-} & Base56-57-type Tail<T extends any[]> = ((...t: T) => any) extends (58- _: any,59- ...tail: infer TT60-) => any61- ? TT62- : []63-64-/** Object types that should never be mapped */65-type AtomicObject =66- | Function67- | WeakMap<any, any>68- | WeakSet<any>69- | Promise<any>70- | Date71- | RegExp72- | Boolean73- | Number74- | String75-76-export type Draft<T> = T extends AtomicObject77- ? T78- : T extends Map<infer K, infer V>79- ? DraftMap<K, V>80- : T extends Set<infer V>81- ? DraftSet<V>82- : T extends object83- ? {-readonly [K in keyof T]: Draft<T[K]>}84- : T85-86-// Inline these in ts 3.787-interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}88-89-// Inline these in ts 3.790-interface DraftSet<V> extends Set<Draft<V>> {}91-92-/** Convert a mutable type into a readonly type */93-export type Immutable<T> = T extends AtomicObject94- ? T95- : T extends Map<infer K, infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableMap<K, V>, "set" | "delete" | "clear">96- ? ImmutableMap<K, V>97- : T extends Set<infer V> // Ideally, but wait for TS 3.7: ? Omit<ImmutableSet<V>, "add" | "delete" | "clear">98- ? ImmutableSet<V>99- : T extends object100- ? {readonly [K in keyof T]: Immutable<T[K]>}101- : T102-103-interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}104-105-interface ImmutableSet<V> extends Set<Immutable<V>> {}106-107-export interface Patch {108- op: "replace" | "remove" | "add"109- path: (string | number)[]110- value?: any111-}112-113-export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void114-115-/** Converts `nothing` into `undefined` */116-type FromNothing<T> = T extends Nothing ? undefined : T117-118-/** The inferred return type of `produce` */119-export type Produced<Base, Return> = Return extends void120- ? Base121- : Return extends Promise<infer Result>122- ? Promise<Result extends void ? Base : FromNothing<Result>>123- : FromNothing<Return>124-125-/**126- * The `produce` function takes a value and a "recipe function" (whose127- * return value often depends on the base state). The recipe function is128- * free to mutate its first argument however it wants. All mutations are129- * only ever applied to a __copy__ of the base state.130- *131- * Pass only a function to create a "curried producer" which relieves you132- * from passing the recipe function every time.133- *134- * Only plain objects and arrays are made mutable. All other objects are135- * considered uncopyable.136- *137- * Note: This function is __bound__ to its `Immer` instance.138- *139- * @param {any} base - the initial state140- * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified141- * @param {Function} patchListener - optional function that will be called with all the patches produced here142- * @returns {any} a new state, or the initial state if nothing was modified143- */144-export interface IProduce {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>>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!159-160- /** 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>>172-173- /** Normal producer */174- <Base, D = Draft<Base>, Return = void>(175- base: Base,176- recipe: (draft: D) => Return,177- listener?: PatchListener178- ): Produced<Base, Return>179-}180-181-/**182- * Like `produce`, but instead of just returning the new state,183- * a tuple is returned with [nextState, patches, inversePatches]184- *185- * Like produce, this function supports currying186- */187-export interface IProduceWithPatches {188- /** Curried producer */189- <190- Recipe extends (...args: any[]) => any,191- Params extends any[] = Parameters<Recipe>,192- T = Params[0]193- >(194- recipe: Recipe195- ): <Base extends Immutable<T>>(196- base: Base,197- ...rest: Tail<Params>198- ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]199- // ^ by making the returned type generic, the actual type of the passed in object is preferred200- // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe201- // 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!202-203- /** Curried producer with initial state */204- <205- Recipe extends (...args: any[]) => any,206- Params extends any[] = Parameters<Recipe>,207- T = Params[0]208- >(209- recipe: Recipe,210- initialState: Immutable<T>211- ): <Base extends Immutable<T>>(212- base?: Base,213- ...rest: Tail<Params>214- ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]215-216- /** Normal producer */217- <Base, D = Draft<Base>, Return = void>(218- base: Base,219- recipe: (draft: D) => Return220- ): [Produced<Base, Return>, Patch[], Patch[]]221-}