Skip to content
Myr

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

4 files+224 added−222 removed

modified:src/internal.ts+2 added-1 removed
added:src/types-external.ts+167 added
1import {Nothing} from "./internal"23type Tail<T extends any[]> = ((...t: T) => any) extends (4 _: any,5 ...tail: infer TT6) => any7 ? TT8 : []910/** Object types that should never be mapped */11type AtomicObject =12 | Function13 | WeakMap<any, any>14 | WeakSet<any>15 | Promise<any>16 | Date17 | RegExp18 | Boolean19 | Number20 | String2122export 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 : T3132// Inline these in ts 3.733interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}3435// Inline these in ts 3.736interface DraftSet<V> extends Set<Draft<V>> {}3738/** Convert a mutable type into a readonly type */39export 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 : T4849interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}5051interface ImmutableSet<V> extends Set<Immutable<V>> {}5253export interface Patch {54 op: "replace" | "remove" | "add"55 path: (string | number)[]56 value?: any57}5859export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void6061/** Converts `nothing` into `undefined` */62type FromNothing<T> = T extends Nothing ? undefined : T6364/** The inferred return type of `produce` */65export type Produced<Base, Return> = Return extends void66 ? Base67 : Return extends Promise<infer Result>68 ? Promise<Result extends void ? Base : FromNothing<Result>>69 : FromNothing<Return>7071/**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 */90export 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!105106 /** 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>>118119 /** Normal producer */120 <Base, D = Draft<Base>, Return = void>(121 base: Base,122 recipe: (draft: D) => Return,123 listener?: PatchListener124 ): Produced<Base, Return>125}126127/**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 */133export 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!148149 /** 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[]]161162 /** 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
deleted:src/types.ts-221 removed
1import {2 SetState,3 ImmerScope,4 ProxyObjectState,5 ProxyArrayState,6 ES5ObjectState,7 ES5ArrayState,8 MapState,9 DRAFT_STATE,10 Nothing11} from "./internal"1213export type Objectish = AnyObject | AnyArray | AnyMap | AnySet14export type ObjectishNoSet = AnyObject | AnyArray | AnyMap1516export type AnyObject = {[key: string]: any}17export type AnyArray = Array<any>18export type AnySet = Set<any>19export type AnyMap = Map<any, any>20export enum Archtype {21 Object,22 Array,23 Map,24 Set25}2627export enum ProxyType {28 ProxyObject,29 ProxyArray,30 ES5Object,31 ES5Array,32 Map,33 Set34}3536export interface ImmerBaseState {37 parent?: ImmerState38 scope: ImmerScope39 modified: boolean40 finalized: boolean41 isManual: boolean42}4344export type ImmerState =45 | ProxyObjectState46 | ProxyArrayState47 | ES5ObjectState48 | ES5ArrayState49 | MapState50 | SetState5152// The _internal_ type used for drafts (not to be confused with Draft, which is public facing)53export type Drafted<Base = any, T extends ImmerState = ImmerState> = {54 [DRAFT_STATE]: T55} & Base5657type Tail<T extends any[]> = ((...t: T) => any) extends (58 _: any,59 ...tail: infer TT60) => any61 ? TT62 : []6364/** Object types that should never be mapped */65type AtomicObject =66 | Function67 | WeakMap<any, any>68 | WeakSet<any>69 | Promise<any>70 | Date71 | RegExp72 | Boolean73 | Number74 | String7576export 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 : T8586// Inline these in ts 3.787interface DraftMap<K, V> extends Map<Draft<K>, Draft<V>> {}8889// Inline these in ts 3.790interface DraftSet<V> extends Set<Draft<V>> {}9192/** Convert a mutable type into a readonly type */93export 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 : T102103interface ImmutableMap<K, V> extends Map<Immutable<K>, Immutable<V>> {}104105interface ImmutableSet<V> extends Set<Immutable<V>> {}106107export interface Patch {108 op: "replace" | "remove" | "add"109 path: (string | number)[]110 value?: any111}112113export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void114115/** Converts `nothing` into `undefined` */116type FromNothing<T> = T extends Nothing ? undefined : T117118/** The inferred return type of `produce` */119export type Produced<Base, Return> = Return extends void120 ? Base121 : Return extends Promise<infer Result>122 ? Promise<Result extends void ? Base : FromNothing<Result>>123 : FromNothing<Return>124125/**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 */144export 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!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>>172173 /** Normal producer */174 <Base, D = Draft<Base>, Return = void>(175 base: Base,176 recipe: (draft: D) => Return,177 listener?: PatchListener178 ): Produced<Base, Return>179}180181/**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 */187export 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!202203 /** 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[]]215216 /** Normal producer */217 <Base, D = Draft<Base>, Return = void>(218 base: Base,219 recipe: (draft: D) => Return220 ): [Produced<Base, Return>, Patch[], Patch[]]221}