types-external.ts
This file
Path
src/types/types-external.ts
188 lines · 5.8 KiB · TypeScript
Last changed by
fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–20why, one step back1import {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 | Promise<any>14 | Date15 | RegExp16 | Boolean17 | Number18 | String1920/**fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
lines 21–37why, one step back21 * If the lib "ES2105.collections" is not included in tsconfig.json,22 * types like ReadonlyArray, WeakMap etc. fall back to `any` (specified nowhere)23 * or `{}` (from the node types), in both cases entering an infite recursion in24 * pattern matching type mappings25 * This type can be used to cast these types to `void` in these cases.26 */27export type IfAvailable<T, Fallback = void> =28 // fallback if any29 true | false extends (T extends never30 ? true31 : false)32 ? Fallback // fallback if empty type33 : keyof T extends never34 ? Fallback // original type35 : T3637/**Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 38–40why, one step back38 * These should also never be mapped but must be tested after regular Map and39 * Set40 */fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 41why, one step back41type WeakReferences = IfAvailable<WeakMap<any, any>> | IfAvailable<WeakSet<any>>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 42why, one step back42fix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
lines 43–44why, one step back43export type WritableDraft<T> = {-readonly [K in keyof T]: Draft<T[K]>}44Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 45–46why, one step back45export type Draft<T> = T extends AtomicObject46 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 47why, one step back47 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 48why, one step back48 ? Map<Draft<K>, Draft<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 49why, one step back49 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 50–53why, one step back50 ? Set<Draft<V>>51 : T extends WeakReferences52 ? T53 : T extends objectfix: Use a named type for Draft object for smaller type declaration filesMichel Weststratebefore adoptiona1a0da0
line 54why, one step back54 ? WritableDraft<T>Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 55–59why, one step back55 : T5657/** Convert a mutable type into a readonly type */58export type Immutable<T> = T extends AtomicObject59 ? Tfix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 60why, one step back60 : T extends IfAvailable<ReadonlyMap<infer K, infer V>> // Map extends ReadonlyMapMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 61why, one step back61 ? ReadonlyMap<Immutable<K>, Immutable<V>>fix: skip ReadonlyMap and ReadonlySet types when not available (#653). Fixes #624Lenz Weberbefore adoption12f4cf8
line 62why, one step back62 : T extends IfAvailable<ReadonlySet<infer V>> // Set extends ReadonlySetMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 63–188why, one step back63 ? ReadonlySet<Immutable<V>>64 : T extends WeakReferences65 ? T66 : T extends object67 ? {readonly [K in keyof T]: Immutable<T[K]>}68 : T6970export interface Patch {71 op: "replace" | "remove" | "add"72 path: (string | number)[]73 value?: any74}7576export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void7778/** Converts `nothing` into `undefined` */79type FromNothing<T> = T extends Nothing ? undefined : T8081/** The inferred return type of `produce` */82export type Produced<Base, Return> = Return extends void83 ? Base84 : Return extends Promise<infer Result>85 ? Promise<Result extends void ? Base : FromNothing<Result>>86 : FromNothing<Return>8788/**89 * The `produce` function takes a value and a "recipe function" (whose90 * return value often depends on the base state). The recipe function is91 * free to mutate its first argument however it wants. All mutations are92 * only ever applied to a __copy__ of the base state.93 *94 * Pass only a function to create a "curried producer" which relieves you95 * from passing the recipe function every time.96 *97 * Only plain objects and arrays are made mutable. All other objects are98 * considered uncopyable.99 *100 * Note: This function is __bound__ to its `Immer` instance.101 *102 * @param {any} base - the initial state103 * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified104 * @param {Function} patchListener - optional function that will be called with all the patches produced here105 * @returns {any} a new state, or the initial state if nothing was modified106 */107export interface IProduce {108 /** Curried producer */109 <110 Recipe extends (...args: any[]) => any,111 Params extends any[] = Parameters<Recipe>,112 T = Params[0]113 >(114 recipe: Recipe115 ): <Base extends Immutable<T>>(116 base: Base,117 ...rest: Tail<Params>118 ) => Produced<Base, ReturnType<Recipe>>119 // ^ by making the returned type generic, the actual type of the passed in object is preferred120 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe121 // 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!122123 /** Curried producer with initial state */124 <125 Recipe extends (...args: any[]) => any,126 Params extends any[] = Parameters<Recipe>,127 T = Params[0]128 >(129 recipe: Recipe,130 initialState: Immutable<T>131 ): <Base extends Immutable<T>>(132 base?: Base,133 ...rest: Tail<Params>134 ) => Produced<Base, ReturnType<Recipe>>135136 /** Normal producer */137 <Base, D = Draft<Base>, Return = void>(138 base: Base,139 recipe: (draft: D) => Return,140 listener?: PatchListener141 ): Produced<Base, Return>142}143144/**145 * Like `produce`, but instead of just returning the new state,146 * a tuple is returned with [nextState, patches, inversePatches]147 *148 * Like produce, this function supports currying149 */150export interface IProduceWithPatches {151 /** Curried producer */152 <153 Recipe extends (...args: any[]) => any,154 Params extends any[] = Parameters<Recipe>,155 T = Params[0]156 >(157 recipe: Recipe158 ): <Base extends Immutable<T>>(159 base: Base,160 ...rest: Tail<Params>161 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]162 // ^ by making the returned type generic, the actual type of the passed in object is preferred163 // over the type used in the recipe. However, it does have to satisfy the immutable version used in the recipe164 // 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!165166 /** Curried producer with initial state */167 <168 Recipe extends (...args: any[]) => any,169 Params extends any[] = Parameters<Recipe>,170 T = Params[0]171 >(172 recipe: Recipe,173 initialState: Immutable<T>174 ): <Base extends Immutable<T>>(175 base?: Base,176 ...rest: Tail<Params>177 ) => [Produced<Base, ReturnType<Recipe>>, Patch[], Patch[]]178179 /** Normal producer */180 <Base, D = Draft<Base>, Return = void>(181 base: Base,182 recipe: (draft: D) => Return183 ): [Produced<Base, Return>, Patch[], Patch[]]184}185186// Fixes #507: bili doesn't export the types of this file if there is no actual source in it..187// hopefully it get's tree-shaken away for everyone :)188export function never_used() {}