types-external.ts
This file
Path
src/types/types-external.ts
256 lines · 8.0 KiB · TypeScript
Last changed by
fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–2why, one step back1import {Nothing} from "../internal"2feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 3why, one step back3type AnyFunc = (...args: any[]) => anyMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 4why, one step back4fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 5–6why, one step back5type PrimitiveType = number | string | boolean6Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 7why, one step back7/** Object types that should never be mapped */fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
line 8why, one step back8type AtomicObject = Function | Promise<any> | Date | RegExpMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 9–10why, one step back910/**fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
lines 11–27why, one step back11 * If the lib "ES2105.collections" is not included in tsconfig.json,12 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)13 * or `{}` (from the node types), in both cases entering an infite recursion in14 * pattern matching type mappings15 * This type can be used to cast these types to `void` in these cases.16 */17export type IfAvailable<T, Fallback = void> =18 // fallback if any19 true | false extends (T extends never20 ? true21 : false)22 ? Fallback // fallback if empty type23 : keyof T extends never24 ? Fallback // original type25 : T2627/**Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 28–30why, one step back28 * These should also never be mapped but must be tested after regular Map and29 * Set30 */fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 31why, one step back31type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 32why, one step back32fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
lines 33–34why, one step back33export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}34fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
line 35why, one step back35export type Draft<T> = T extends PrimitiveTypeMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 36why, one step back36 ? Tfix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 37–38why, one step back37 : T extends AtomicObject38 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 39why, one step back39 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 40why, one step back40 ? Map<Draft<K>, Draft<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 41why, one step back41 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 42–45why, one step back42 ? Set<Draft<V>>43 : T extends WeakReferences44 ? T45 : T extends objectfix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
line 46why, one step back46 ? WritableDraft<T>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 47–49why, one step back47 : T4849/** Convert a mutable type into a readonly type */fix: Add a type-checking fast path for primitive types (#755)Andrew Caseybefore adoptiond395efe
lines 50–52why, one step back50export type Immutable<T> = T extends PrimitiveType51 ? T52 : T extends AtomicObjectMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 53why, one step back53 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 54why, one step back54 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 55why, one step back55 ? ReadonlyMap<Immutable<K>, Immutable<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 56why, one step back56 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 57–82why, one step back57 ? ReadonlySet<Immutable<V>>58 : T extends WeakReferences59 ? T60 : T extends object61 ? {readonly [K in keyof T]: Immutable<T[K]>}62 : T6364export interface Patch {65 op: "replace" | "remove" | "add"66 path: (string | number)[]67 value?: any68}6970export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7172/** Converts `nothing` into `undefined` */73type FromNothing<T> = T extends Nothing ? undefined : T7475/** The inferred return type of `produce` */76export type Produced<Base, Return> = Return extends void77 ? Base78 : Return extends Promise<infer Result>79 ? Promise<Result extends void ? Base : FromNothing<Result>>80 : FromNothing<Return>8182/**feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 83–155why, one step back83 * Utility types84 */85type PatchesTuple<T> = readonly [T, Patch[], Patch[]]8687type ValidRecipeReturnType<State> =88 | State89 | void90 | undefined91 | (State extends undefined ? Nothing : never)9293type ValidRecipeReturnTypePossiblyPromise<State> =94 | ValidRecipeReturnType<State>95 | Promise<ValidRecipeReturnType<State>>9697type PromisifyReturnIfNeeded<98 State,99 Recipe extends AnyFunc,100 UsePatches extends boolean101> = ReturnType<Recipe> extends Promise<any>102 ? Promise<UsePatches extends true ? PatchesTuple<State> : State>103 : UsePatches extends true104 ? PatchesTuple<State>105 : State106107/**108 * Core Producer inference109 */110type InferRecipeFromCurried<Curried> = Curried extends (111 base: infer State,112 ...rest: infer Args113) => any // extra assertion to make sure this is a proper curried function (state, args) => state114 ? ReturnType<Curried> extends State115 ? (116 draft: Draft<State>,117 ...rest: Args118 ) => ValidRecipeReturnType<Draft<State>>119 : never120 : never121122type InferInitialStateFromCurried<Curried> = Curried extends (123 base: infer State,124 ...rest: any[]125) => any // extra assertion to make sure this is a proper curried function (state, args) => state126 ? State127 : never128129type InferCurriedFromRecipe<130 Recipe,131 UsePatches extends boolean132> = Recipe extends (draft: infer DraftState, ...args: infer RestArgs) => any // verify return type133 ? ReturnType<Recipe> extends ValidRecipeReturnTypePossiblyPromise<DraftState>134 ? (135 base: Immutable<DraftState>,136 ...args: RestArgs137 ) => PromisifyReturnIfNeeded<DraftState, Recipe, UsePatches> // N.b. we return mutable draftstate, in case the recipe's first arg isn't read only, and that isn't expected as output either138 : never // incorrect return type139 : never // not a function140141type InferCurriedFromInitialStateAndRecipe<142 State,143 Recipe,144 UsePatches extends boolean145> = Recipe extends (146 draft: Draft<State>,147 ...rest: infer RestArgs148) => ValidRecipeReturnTypePossiblyPromise<State>149 ? (150 base?: State | undefined,151 ...args: RestArgs152 ) => PromisifyReturnIfNeeded<State, Recipe, UsePatches>153 : never // recipe doesn't match initial state154155/**Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 156–174why, one step back156 * The `produce` function takes a value and a "recipe function" (whose157 * return value often depends on the base state). The recipe function is158 * free to mutate its first argument however it wants. All mutations are159 * only ever applied to a __copy__ of the base state.160 *161 * Pass only a function to create a "curried producer" which relieves you162 * from passing the recipe function every time.163 *164 * Only plain objects and arrays are made mutable. All other objects are165 * considered uncopyable.166 *167 * Note: This function is __bound__ to its `Immer` instance.168 *169 * @param {any} base - the initial state170 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified171 * @param {Function} patchListener - optional function that will be called with all the patches produced here172 * @returns {any} a new state, or the initial state if nothing was modified173 */174export interface IProduce {feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 175–179why, one step back175 /** Curried producer that infers the recipe from the curried output function (e.g. when passing to setState) */176 <Curried>(177 recipe: InferRecipeFromCurried<Curried>,178 initialState?: InferInitialStateFromCurried<Curried>179 ): CurriedMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 180why, one step back180feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 181–209why, one step back181 /** Curried producer that infers curried from the recipe */182 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<183 Recipe,184 false185 >186187 /** Curried producer that infers curried from the State generic, which is explicitly passed in. */188 <State>(189 recipe: (190 state: Draft<State>,191 initialState: State192 ) => ValidRecipeReturnType<State>193 ): (state?: State) => State194 <State, Args extends any[]>(195 recipe: (196 state: Draft<State>,197 ...args: Args198 ) => ValidRecipeReturnType<State>,199 initialState: State200 ): (state?: State, ...args: Args) => State201 <State>(recipe: (state: Draft<State>) => ValidRecipeReturnType<State>): (202 state: State203 ) => State204 <State, Args extends any[]>(205 recipe: (state: Draft<State>, ...args: Args) => ValidRecipeReturnType<State>206 ): (state: State, ...args: Args) => State207208 /** Curried producer with initial state, infers recipe from initial state */209 <State, Recipe extends Function>(Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 210why, one step back210 recipe: Recipe,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 211–212why, one step back211 initialState: State212 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, false>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 213–214why, one step back213214 /** Normal producer */feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 215why, one step back215 <Base, D = Draft<Base>>( // By using a default inferred D, rather than Draft<Base> in the recipe, we can override it.Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 216why, one step back216 base: Base,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 217why, one step back217 recipe: (draft: D) => ValidRecipeReturnType<D>,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 218why, one step back218 listener?: PatchListenerfeature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 219why, one step back219 ): Basefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 220–226why, one step back220221 /** Promisified dormal producer */222 <Base, D = Draft<Base>>(223 base: Base,224 recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,225 listener?: PatchListener226 ): Promise<Base>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 227–235why, one step back227}228229/**230 * Like `produce`, but instead of just returning the new state,231 * a tuple is returned with [nextState, patches, inversePatches]232 *233 * Like produce, this function supports currying234 */235export interface IProduceWithPatches {feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 236–238why, one step back236 // Types copied from IProduce, wrapped with PatchesTuple237 <Recipe extends AnyFunc>(recipe: Recipe): InferCurriedFromRecipe<Recipe, true>238 <State, Recipe extends Function>(Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 239why, one step back239 recipe: Recipe,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 240–242why, one step back240 initialState: State241 ): InferCurriedFromInitialStateAndRecipe<State, Recipe, true>242 <Base, D = Draft<Base>>(Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 243why, one step back243 base: Base,fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d
line 244why, one step back244 recipe: (draft: D) => ValidRecipeReturnType<D>,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 245why, one step back245 listener?: PatchListenerfix: produceWithPatches should not wrap result in Immutable, fixes #850, #881Michel Weststratebefore adoptiond8f2636
line 246why, one step back246 ): PatchesTuple<Base>feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
lines 247–248why, one step back247 <Base, D = Draft<Base>>(248 base: Base,fix: consistent recipe return type in produceWithPatches (#934)Myles Jbefore adoption220d61d
line 249why, one step back249 recipe: (draft: D) => Promise<ValidRecipeReturnType<D>>,feature: Improved typescript typesMichel Weststratebefore adoption2c2f30e
line 250why, one step back250 listener?: PatchListenerfix: incorrect return type for async produceWithPatches (#933)Myles Jbefore adoption9f7623d
line 251why, one step back251 ): Promise<PatchesTuple<Base>>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 252–256why, one step back252}253254// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..255// hopefully it get's tree-shaken away for everyone :)256export function never_used() {}